编写一个程序,输入一个三角形的边长,输出这个三角形的面积和周长。
周长=a+b+c;
面积计算公式:s*(s-a)*(s-b)*(s*c)的开根号
s=1/2*(a+b+c)
package test1;
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Length of first side:");
double side1 = Double.parseDouble(in.nextLine());
System.out.print("Enter Length of second side:");
double side2 = Double.parseDouble(in.nextLine());
System.out.print("Enter Length of thrid side:");
double side3 = Double.parseDouble(in.nextLine());
if (((side1 + side2) < side3) || ((side2 + side3) < side1) || ((side1 + side3) < side2)) {
System.out.println("The input does not describe a triangle.");
} else {
double perimeter=side1+side2+side3;
double semiperimeter=0.5* perimeter;
double temp=semiperimeter*(semiperimeter-side1)*(semiperimeter-side2)*(semiperimeter-side3);
double area=Math.sqrt(temp);
System.out.println("周长是"+perimeter);
System.out.println("面积是"+area);
}
}
}
测试:
Enter Length of first side:3.1
Enter Length of second side:4.2
Enter Length of thrid side:5.3
周长是12.600000000000001
面积是6.506612021628465