我有这样的代码,我想赶上字母例外,但它一直有这些错误:的Java InputMismatchException时
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)
at exercise_one.Exercise.main(Exercise.java:17)
这里是我的代码:
System.out.print("Enter the number of students: ");
students = input.nextInt();
while (students <= 0) {
try {
System.out.print("Enter the number of students: ");
students = input.nextInt();
}
catch (InputMismatchException e) {
System.out.print("Enter the number of students");
}
}
第一个'students = input.nextInt();'不在'try'块中,而你输入的东西不能存储在'int'中。 – jlordo
是的,似乎是这种情况,但我怎样才能同时检查(字母和负数除外)? –
简单。只需删除您发布的前两行代码即可。 – jlordo