Java三角形三边检测异常

5 篇文章 0 订阅

Outcome

1.出现异常则会出现警告字样和检测异常完后的提示。

2.无异常则不会出现警告字样,只会出现检测异常完后的提示。

注:使用throw一定要用throws关键字配合在前面向方法外抛出,对异常进行声明,方便后面捕获,最后根据自己的需要与否使用try-catch对异常进行加工处理。

Code

import java.util.Scanner;

class IllegalTriangleException extends Exception{     //异常类
    public IllegalTriangleException(){
        super();
    }
    public IllegalTriangleException(String message){
        super(message);
    }
}

public class Triangle{
    public static String detection()  throws IllegalTriangleException {
        return "\n警告!";
    }
    public static String detection1(double side1,double side2,double side3)  throws IllegalTriangleException {
        if((side1+side2>side3) & (side1+side3>side2) & (side2+side3>side1))
            throw new IllegalTriangleException("这三条边的边长的设置  符合  【三角形任意两边之和总大于第三边】  的规则。");
        else
            return "这三条边的边长的设置  违反  【三角形任意两边之和总大于第三边】  的规则。";
    }
    public static void main(String[] args){

        try {
            System.out.println("请输入三角形三条边各自的长度:");
            Scanner input=new Scanner(System.in);
            double side1=input.nextDouble();
            double side2=input.nextDouble();
            double side3=input.nextDouble();
            String outcome=detection();
            String outcome1=detection1(side1,side2,side3);
            System.out.println(outcome);
            System.out.println(outcome1);

        }catch (IllegalTriangleException e){
            System.out.println(e.getMessage());
        }
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据题目要求,我们需要定义三角形类 Triangle 和异常类 IllegalTriangleException ,并在 Triangle 构造函数中进行异常处理。具体实现如下: ``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); double side1, side2, side3; side1 = in.nextDouble(); side2 = in.nextDouble(); side3 = in.nextDouble(); try { Triangle triangle = new Triangle(side1, side2, side3); } catch (IllegalTriangleException e) { System.out.println(e.getMessage()); } } } class Triangle { private double side1; private double side2; private double side3; public Triangle(double side1, double side2, double side3) throws IllegalTriangleException { if (side1 + side2 <= side3 || side1 + side3 <= side2 || side2 + side3 <= side1) { throw new IllegalTriangleException("三角形两边之和必须大于第三边"); } this.side1 = side1; this.side2 = side2; this.side3 = side3; } } class IllegalTriangleException extends Exception { public IllegalTriangleException(String message) { super(message); } } ``` 在 Triangle 构造函数中,我们对三角形三边进行检查,如果出现两条边之和小于等于第三边,则抛出 IllegalTriangleException 异常对象,并将错误信息传递给该异常对象。在 main 函数中,我们使用 try-catch 语句对异常进行捕获,并输出错误信息。 注意,在 Triangle 构造函数中,我们使用了 throws 关键字,表示该函数可能会抛出 IllegalTriangleException 异常。在调用 Triangle 构造函数时,我们需要使用 try-catch 语句进行异常处理

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ZYT_庄彦涛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值