新手java练习题(21-25)

产生一个圆心在(0, 0 )、 半径为 50cm 的圆上面的三个随机点,
*显示由这三个随机点组成的三角形的周长和面积。
*要求:求任意两个点之间的距离、三角形的周长和面积

public abstract class class21 {

        public static void main(String[] args)
        {
            System.out.println("要求:计算圆上任意三点构成的三角形的各个角度值");
            final int r = 50;

            System.out.println("半径r=50,圆心为(0,0)");
            // 随机产生0-2之间的弧度
            double a1 = Math.random() * 2;
            double a2 = Math.random() * 2;
            double a3 = Math.random() * 2;
            System.out.println("随机产生的三个辅助角度为:");
            System.out.printf("a1=%1.2fπ a2=%1.2fπ a3=%1.2fπ", a1, a2, a3);
            // 产生三个坐标点
            double x1 = r * Math.cos(a1);
            double y1 = r * Math.sin(a1);
            double x2 = r * Math.cos(a2);
            double y2 = r * Math.sin(a2);
            double x3 = r * Math.cos(a3);
            double y3 = r * Math.sin(a3);
            System.out.println("\n随机产生的三个点的坐标为:");
            System.out.printf("(%1.2f , %1.2f)\n", x1, y1);
            System.out.printf("(%1.2f , %1.2f)\n", x2, y2);
            System.out.printf("(%1.2f , %1.2f)\n", x3, y3);
            // 计算三角形各个边长
            double s1 = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
            double s2 = Math.sqrt(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3, 2));
            double s3 = Math.sqrt(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2));
            double sum = s1+s2+s3;double p = sum/2;//计算周长和求周长/2
            System.out.println("三角形边长为:");
            System.out.printf("s1=%1.2f s2=%1.2f s3=%1.2f\n", s1,s2,s3);
            System.out.println("三角形周长为:");
            System.out.printf("sum=%1.2f",sum);
             // 计算三角形面积
            double ss = Math.sqrt(p*(p-s1)*(p-s2)*(p-s3));//海伦公式
            System.out.println("\n三角形面积为:");
            System.out.println(ss);

        }

}

你可能感兴趣的:(Java练习题)