定义三个整数变量a,b,c 判断能否构成一个三角形
如果不能请抛出一个自定义异常
public class Triangle {
public static void main(String[] args) {
try {
isTriangle();
} catch (TriangelException e) {
System.out.println(e.getMessage());
}
}
public static void isTriangle() throws TriangelException {
//输入三条边的长度
System.out.println("输入三边长度:");
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
//判断是否构成三角形
if((a+b)>c && (a+c)>b && (b+c)>a){
System.out.println("构成三角形");
}else {
throw new TriangelException("无法构成三角形");
}
}
}
//自定义异常
public class TriangelException extends Exception{
public TriangelException(String massage) {
super(massage);
}
}