test.java
import java.util.Scanner;
public class test {
public static void main (String[] args) {
Scanner in =new Scanner(System.in );
int i,j,k;
System.out .println("请输入三角形边长:" );
i=in .nextInt();
j=in .nextInt();
k=in .nextInt();
try {
triangle(i,j,k);
}catch (IllegalArgumentException e) {
System.out .println("不能构成三角形" );
e.printStackTrace();
}
}
public static void triangle (int a,int b,int c) throws IllegalArgumentException{
int t;
if (b>a){
t=a;a=b;b=t;
}
if (c>a){
t=a;a=c;c=t;
}
if (b>c){
t=b;b=c;c=t;
}
if (b+c<=a){
throw new IllegalArgumentException();
}
else {
System.out .println("三角形边长为:" +a+" " +b+" " +c);
}
}
}
运行结果:
请输入三角形边长:
1 2 3
不能构成三角形
java.lang .IllegalArgumentException
at test.triangle (test.java :32 )
at test.main (test.java :13 )