为什么不是按预期方式工作?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