给这段代码增加详细注释import java.util.Scanner; public class DoubleDivideOperation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { System.out.print("请输入被除数:"); double dividend = Double.parseDouble(scanner.nextLine()); System.out.print("请输入除数:"); double divisor = Double.parseDouble(scanner.nextLine()); if (divisor == 0) { throw new ArithmeticException("除数不能为0"); } System.out.println("结果为:" + (dividend / divisor)); } catch (NumberFormatException e) { System.out.println("数字格式异常:" + e.getMessage()); } catch (ArithmeticException e) { System.out.println("除数为0异常:" + e.getMessage()); } catch (Exception e) { System.out.println("通用型异常:" + e.getMessage()); } } }
时间: 2024-02-14 22:09:40 浏览: 66
// 导入Scanner类
import java.util.Scanner;
// 定义DoubleDivideOperation类
public class DoubleDivideOperation {
// 定义主函数
public static void main(String[] args) {
// 创建Scanner对象
Scanner scanner = new Scanner(System.in);
try {
// 提示用户输入被除数
System.out.print("请输入被除数:");
// 读取用户输入
double dividend = Double.parseDouble(scanner.nextLine());
// 提示用户输入除数
System.out.print("请输入除数:");
// 读取用户输入
double divisor = Double.parseDouble(scanner.nextLine());
// 判断除数是否为0
if (divisor == 0) {
// 如果为0,则抛出除数不能为0的异常
throw new ArithmeticException("除数不能为0");
}
// 输出结果
System.out.println("结果为:" + (dividend / divisor));
} catch (NumberFormatException e) {
// 捕获数字格式异常,并输出异常信息
System.out.println("数字格式异常:" + e.getMessage());
} catch (ArithmeticException e) {
// 捕获除数为0异常,并输出异常信息
System.out.println("除数为0异常:" + e.getMessage());
} catch (Exception e) {
// 捕获通用型异常,并输出异常信息
System.out.println("通用型异常:" + e.getMessage());
}
}
}
阅读全文