class Triangle {
private int a;
private int b;
private int c;
public Triangle(int a,int b,int c){
/*if((a+b>c)&&(a+c>b)&&(b+c>a)){
this.a = a;
this.b = b;
this.c = c;
} else {
System.out.println("对不起,亲,您输入的数字无法构成三角形!");
throw new RuntimeException("拜拜!");
//return ;
}*/
if((a+b<=c)||(a+c<=b)||(b+c<=a)){
System.out.println("对不起,亲,您输入的数字无法构成三角形!");
throw new RuntimeException("拜拜!");
} else {
this.a = a;
this.b = b;
this.c = c;
}
}
//获取三角形的周长
public int getLength(){
return a+b+c;
}
//获取三角形的面积
public double getArea(){
double temp = (a+b+c)/2.0;
//double temp = (getLength())/2.0;
return Math.sqrt((temp-a)*(temp-b)*(temp-c)*temp);
}
public static void main(String[] args){
Triangle t = new Triangle(1,2,3);
System.out.println("您输入的三角形的周长为:"+t.getLength()+",面积为:"+t.getArea());
}
} |
|