使用random生成double类型的三个数指定为三角形的三个边,并编写函数计算面积,并求出三个角各角的度数,要求使用Math类中的方法进行运算。
package homework_seven;
public class three {
static double area(double a,double b,double c) {//计算面积的函数
double x;
x=(a+b+c)/2.0;
return Math.sqrt(x*(x-a)*(x-b)*(x-c));
}
static void angle(double a,double b,double c) {//求三个角的度数
double x=Math.toDegrees(Math.acos((b*b+c*c-a*a)/(2.0*b*c)));
double y=Math.toDegrees(Math.acos((b*b-a*a-c*c)/(-2.0*a*c)));
double z=Math.toDegrees(Math.acos((c*c-b*b-a*a)/(-2.0*a*b)));
System.out.println("三个角的度数分别为:"+x+" "+y+" "+z);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
double a,b,c;//定义三角形的三个边
a=Math.random()*10;//用随机数进行赋值
b=Math.random()*10;
c=Math.random()*10;
System.out.println(a);
if (a+b>c && b+c>a && a+c>b){//判断三角形是否存在
double i =area(a,b,c);
System.out.println("面积为:"+i);//存在则输出面积
angle(a,b,c);//求三个角的度数
}
else {
System.out.println("三角形不存在,请重新尝试!");
}
}
}