gpt4 book ai didi

Java - 除以零时无法捕获 ArithmeticException

转载 作者:搜寻专家 更新时间:2023-11-01 01:10:05 25 4
gpt4 key购买 nike

<分区>

我一定是做了什么蠢事。但我似乎无法弄清楚为什么这个简单的代码不起作用。 InputMismatchException 有效,但 ArithmeticException 永远不会被捕获。

import java.util.InputMismatchException;
import java.util.Scanner;
public class SubChap02_DivisionByZero {
public static double quotient(double num, double denum) throws ArithmeticException {
return num / denum;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double num, denum, result;
boolean continueLoop = true;
do {
try {
System.out.printf("Please enter the numerator: ");
num = scanner.nextFloat();
System.out.printf("Please enter the denumerator: ");
denum = scanner.nextFloat();
result = quotient(num, denum);
continueLoop = false;
System.out.printf("THIS: %.2f/%.2f is %.2f\n", num, denum, result);
scanner.close();
} catch (ArithmeticException arithmeticException) {
System.err.printf("Exception : %s\n", arithmeticException);
scanner.nextLine();
System.out.println("You can try again!");
} catch (InputMismatchException inputMismatchException) {
System.err.printf("Exception : %s\n", inputMismatchException);
scanner.nextLine();
System.out.println("You can try again!");
}
} while (continueLoop == true);
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com