我試圖讀取一個開關盒選項的數字,但我堅持一個例外。我會嘗試更好地解釋代碼中的問題:無限循環與java代碼,掃描器對象緩衝區可能是
do{
try{
loop=false;
int op=teclado.nextInt();
//I tryed a teclado.nextLine() here cause i saw in other Q but didn't work
}
catch(InputMismatchException ex){
System.out.println("Invalid character. Try again.");
loop=true;//At the catch bolck i change the loop value
}
}while(loop);//When loop is true it instantly go to the catch part over and over again and never ask for an int again
當我鍵入它完美int,但異常使它重新開始。第二次,程序不要求int(我認爲它可能是一個緩衝區,我需要C中的fflush(stdin)
),並且緩衝區開始像瘋了一樣寫。
您正在閱讀的文件的格式是什麼?這可能是它絆腳石不是一個整數(這就是爲什麼會引發'InputMismatchException'。 – Makoto 2012-04-28 04:57:44
我正在從鍵盤讀取「Scanner teclado = new Scanner(System.in);」 – 2012-04-28 04:58:42
您是否使用了同樣的掃描器在進入do-while循環之前? – 2012-04-28 05:02:59