1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| class Triangle { double[] len; double[] degree; private double area, cir;
Triangle(double num1, double num2, double num3) { if (num1 + num2 <= num3 || num1 + num3 <= num2 || num2 + num3 <= num1) { System.out.println("无法构成三角形"); len = null; return; }
len = new double[3]; degree = new double[3]; len[0] = num1; len[1] = num2; len[2] = num3; cir = len[0] + len[1] + len[2]; double p = cir / 2; area = Math.sqrt(p * (p - len[0]) * (p - len[1]) * (p - len[2])); double a = Math.pow(len[0], 2), b = Math.pow(len[1], 2), c = Math.pow(len[2], 2); degree[0] = Math.toDegrees(Math.acos((a + b - c) / (2 * len[0] * len[1]))); degree[1] = Math.toDegrees(Math.acos((a + c - b) / (2 * len[0] * len[2]))); degree[2] = Math.toDegrees(Math.acos((b + c - a) / (2 * len[1] * len[2]))); }
double getArea() { return area; }
double getCir() { return cir; }
}
public class Main {
public static void main(String[] args) { Triangle t = new Triangle(3, 4, 5); System.out.println("三角形的面积为:" + t.getArea() + "," + "周长为:" + t.getCir()); System.out.println("三角度数为:" + t.degree[0] + "," + t.degree[1] + "," + t.degree[2]); } }
|