如果用户不小心输入了错误的输入(字母),我希望程序一次又一次地循环,直到给出正确的输入,但是使用下面的代码我有一些错误。有任何想法吗?
catch (InputMismatchException e) {
input.nextLine();
while (!input.hasNextInt()) {
System.out.print("Enter the number of people in the circle: ");
numberOfPeople = input.nextInt();
}
}
错误输出:
Exception in thread "main" java.util.InputMismatchException at
java.util.Scanner.throwFor(Scanner.java:840) at
java.util.Scanner.next(Scanner.java:1461) at
java.util.Scanner.nextInt(Scanner.java:2091) at
java.util.Scanner.nextInt(Scanner.java:2050)