try {
System.out.println("how many times");
rollnumber = scanner.nextInt();
nigh=2;
}catch (Exception e){
System.out.print("invalid. re-enter");
}
}while (nigh==1);
它会不断打印无限多次"无效重复输入"
答案 0 :(得分:1)
你进入一个无限循环,因为scanner.nextInt();
不会消耗错误输入中的字符。更改catch
子句,如下所示:
try {
... // Your code
} catch (Exception e){
System.out.print("invalid. re-enter");
scanner.nextLine();
}