Java 输入两个数,输出两个数的商,自定义异常类NegativeException和ZeroException用于检测输入的除数为负数和零时,抛出异常。

题目:

【问题描述】

输入两个数,输出两个数的商,自定义异常类NegativeException和ZeroException用于检测输入的除数为负数和零时,抛出异常。

【输入形式】

请输入第一个数:

请输入第二个数:

【输出形式】

商是:

【输入输出样例1】

Please input first number:

5

Please input second number:

2

Divisor is : 2.5

finally!

【输入输出样例2】

Please input first number:

5

Please input second number:

-2

The divisor, -2, could not be negative!

finally!

【输入输出样例3】

Please input first number:

5

Please input second number:

0

The divisor, 0,could not be zero!

finally!

代码:

import java.util.Scanner;

class NegativeException extends Exception{
	String message;
	public NegativeException(int n){
		message = "The divisor, "+n+", could not be negative!";
	}
	
}
class ZeroException extends Exception{
	String message;
	public ZeroException(int m){
		message = "The divisor, "+m+",could not be zero!";
	}
	
}

class My{
	public My(int m,int n)throws Exception{
		if(n<0) {
			throw new NegativeException(n);
		}
		else if(n==0) {
			throw new ZeroException(n);
		}
		else {
			System.out.println("Divisor is : "+(float)m/n);
		}
	}
	
}

public class zhong{
	public static void main(String[] args) throws Exception {
		Scanner in=new Scanner(System.in);
		System.out.println("Please input first number: ");
		int m=in.nextInt();
		System.out.println("Please input second number: ");
		int n=in.nextInt();
		try {
			new My(m,n);
		}
		catch(NegativeException e ) {
			System.out.println(e.message);
		}
		catch(ZeroException e ) {
			System.out.println(e.message);
		}
		System.out.println("finally!");
	}
}

  • 6
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
下面是实现上述功能的Java代码: ```java class NegativeException extends Exception { public NegativeException(String message) { super(message); } } class ZeroException extends Exception { public ZeroException(String message) { super(message); } } public class Calculator { public static void main(String[] args) { int divisor = 0, dividend = 0; try { Scanner sc = new Scanner(System.in); System.out.print("请输入除数:"); dividend = sc.nextInt(); System.out.print("请输入除数:"); divisor = sc.nextInt(); if (divisor < 0) { throw new NegativeException("除数不能为负数!"); } else if (divisor == 0) { throw new ZeroException("除数不能为零!"); } else { System.out.println("为:" + dividend / divisor); } } catch (InputMismatchException e) { System.out.println("输入的不是整数!"); } catch (NegativeException e) { System.out.println(e.getMessage()); } catch (ZeroException e) { System.out.println(e.getMessage()); } catch (Exception e) { System.out.println("出现异常!"); } } } ``` 程序中定义了自定义异常`NegativeException`和`ZeroException`,用于检测输入除数是否为负数和零。在`main`函数中,首先获取用户输入的被除数除数,并对除数进行判断。如果除数负数,则抛出`NegativeException`异常,如果除数为零,则抛出`ZeroException`异常,否则输出。在异常处理中,如果输入的不是整数,则抛出`InputMismatchException`异常;如果捕获到`NegativeException`和`ZeroException`异常,则输出异常信息;如果捕获到其他异常,则输出"出现异常!"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱犯错的小z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值