Java基础 编写应用程序,从命令行输入两个整数参数,求他们的商,要求程序中捕获所有可能发生的异常

题目:
编写应用程序,从命令行输入两个整数参数,求他们的商,要求程序中捕获所有可能发生的异常

异常

  1. NumberFormatException 数字格式异常,判读输入的数字是否是数字
  2. ArithmeticException 算术异常,判断除数是否为0
public class TestParameter {

	public static void main(String[] args) {

		// ArithmeticException 算数异常
		// NumberFormatException 数字格式异常
		Scanner scanner = new Scanner(System.in);
		while (true) {
			try {
				System.out.println("请输入第一个数:");
				int n1 = Integer.parseInt(scanner.nextLine());
				System.out.println("请输入第二个数:");
				int n2 = Integer.parseInt(scanner.nextLine());
				int n3 = n1 / n2;
				System.out.println(n1 + " / " + n2 + " = " + n3);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				continue;
			} catch (ArithmeticException e) {
				e.printStackTrace();
				continue;
			} finally {
				System.out.println("每次都输出的!");
			}
		}
	}
}

运行结果:
在这里插入图片描述

每天都在进步!!!

  • 11
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值