java在遇到try-catch问题时会做些什么
我正在努力让我的do while block正确运行。里面有一个只起部分作用的试捕块。我希望它在输入Int以外的内容时捕获异常(InputMismatchException
),并在除以0时捕获异常。如果其中一个捕获发生,则目的是通过do while循环返回到重试。目前,它正在为ArithmeticException
工作,但不为InputMismatchException
工作。当我输入字符而不是整数时,它似乎会不停地循环。请帮忙。我不明白为什么一个有效,而另一个无效
int div;
boolean check = true;
while (check) {
boolean result = true;
do {
try {
System.out.println("The following operation will divide the first number by the second.");
System.out.println("Enter first number.");
example.a = user_input.nextInt();
System.out.println("Enter second number.");
example.b = user_input.nextInt();
div = example.div();
System.out.println("The result of this division is " + div);
result = true;
} catch (InputMismatchException e) {
System.out.println("That is not a number. Please try again.");
result = false;
} catch (ArithmeticException e) {
System.out.println("Division by zero.");
result = false;
}
} while (result = true);
共 (0) 个答案