我一直對這個代碼的項目,我不斷收到錯誤「java.util.InputMismatchException」。我已經搜索並發現了類似的問題,但我沒有得到答案如何適用於我的代碼。我知道我正確地輸入了輸入,所以沒有了。另外,我一直試圖重新格式化我的代碼,但它似乎使情況變得更糟。我很抱歉,如果這顯而易見,我不認識它,我只是開始編碼。謝謝你的耐心。我不斷收到錯誤「java.util.InputMismatchException」在我的代碼
下面是完整的錯誤消息:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at songBottlesOfBeer.BottlesOfBeer.main(BottlesOfBeer.java:47)
這是我的完整代碼段:
package songBottlesOfBeer;
import java.util.Scanner;
public class BottlesOfBeer {
private static Scanner bottles;
public static void number(int n) {
if (n>1) {
System.out.print(n+" bottles of beer on the wall, "+
n+" bottles of beer, ya' take one down, "+
"ya' pass it around, ");
n=n-1 ;
System.out.println(n+" bottles of beer on the wall.");
number(n);
}else{
if(n==1) {
System.out.print(n+" bottle of beer on the wall, "+
n+" bottle of beer, ya' take one down, "+
"ya' pass it around, ");
n=n-1 ;
System.out.println(n +" bottles of beer on the wall.");
number(n);
}else{
System.out.println("No more bottles of beer on the wall, " +
"no bottles of beer, ya' can't take one down, "
+ "ya' can't pass it around, 'cause there are"
+ " no more bottles of beer on the wall!");
}
}
}
public static void main(String[] args) {
bottles = new Scanner(System.in);
bottles.useDelimiter("\n");
System.out.println("Enter the starting number of "
+ "bottles in the song "
+ "'99 Bottles of Beer on the Wall':");
number(bottles.nextInt());
}
}
誤差位於數(bottles.nextInt());
唯一的例外發生在'BottlesOfBeer.java:47',這是哪一行? – user1803551 2014-10-18 18:57:03
@ user1803551我猜'數字(bottles.nextInt());' – 2014-10-18 18:57:47
@GáborBakos是的,但我們不應該猜測。 OP應該總是知道在代碼中發生錯誤的位置。 – user1803551 2014-10-18 18:58:40