我面对java.util.InputMismatchException;
inputmismatchexception:进入无限循环?
我赶上InputMismatchException时,但我不明白为什么它会进入无限循环以第一输入错误后,输出继续这样:
enter two integers
exception caught
这样下去重复
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int flag = 0;
while (flag != 1) {
try {
System.out.println("enter two integers");
int a = sc.nextInt();
int b = sc.nextInt();
int result = a + b;
flag = 1;
System.out.println("ans is" + result);
} catch (NumberFormatException e) {
System.out.println("exception caught");
} catch (InputMismatchException e) {
System.out.println("exception caught");
}
}
}
@Berger是啊,我不知道OP很希望如何进入他的数据。 –