爲什麼不是按預期方式工作?While循環掃描器
public class FinalTest {
public static void main (String [] args) {
Scanner in = new Scanner(System.in);
int k = 0;
boolean askForInput = true;
while (askForInput) {
System.out.print("Enter an integer: ");
try {
k = in.nextInt();
askForInput = false;
} catch (InputMismatchException e) {
System.out.println("ERR: Not an integer!!");
}
}
}
}
nextInt()
嘗試掃描輸入作爲一個int,如果它不是一個整數,它應該拋出一個異常錯誤:不是整數。什麼錯誤是我爲什麼不提示再次輸入?它只是在屏幕上繼續打印ERR消息。
嘗試添加''in.nextLine();''後打印錯誤消息 –
@mondoteck工作!謝啦! – py9