//13.5 import java.util.Scanner; import java.awt.*; import java.util.*; import javax.swing.*; import javax.swing.border.LineBorder; public class Test { public static void main(String[] args) { double s1 = 0,s2 = 0,s3 = 0; Scanner input = new Scanner(System.in); s1 = input.nextDouble(); s2 = input.nextDouble(); s3 = input.nextDouble(); try {Triangle t1 = new Triangle(s1,s2,s3);} catch(IllegalTriangleException ex){ System.out.println(ex.getMessage()); } } }
Test.java
public class Triangle { private double side1,side2,side3; public Triangle(double side1,double side2,double side3) throws IllegalTriangleException{ if((side1+side2>side3)&&(side1+side3>side2)&&(side2+side3>side1)) System.out.println("valid sides"); else throw new IllegalTriangleException("invalid sides"); } }
Triangle.java
public class IllegalTriangleException extends Exception{ public IllegalTriangleException(String s) { super(s); } }
IllegalTriangleException.java
时间: 2024-12-20 08:00:23