JAVA自学之每日一题(一)

题目要求:编写一个程序,产生一个圆心在(0,0),半径为40的圆上面的三个随机点,显示由这三个随机点组成的三角形的三个角的度数。(提示:产生0-2π之间的一个以弧度为单位的随即角度a,则由这个角度所确定的点为(r*cos(a),r*sin(a))。

public class Test
{

    public static void main(String[] args)
    {
        System.out.println("要求:计算圆上任意三点构成的三角形的各个角度值\n\n");
        final int r = 40;
        final double PI = 3.14159;
        System.out.println("半径r=40,圆心为(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));
        System.out.println("三条边的长度为:");
        System.out.printf("s1=%1.2f s2=%1.2f s3=%1.2f", s1, s2, s3);
        // 计算三角形三个角的度数
        double A = (Math.acos((s1 * s1 - s2 * s2 - s3 * s3) / (-2 * s2 * s3)))
                / PI;
        double B = (Math.acos((s2 * s2 - s1 * s1 - s3 * s3) / (-2 * s1 * s3)))
                / PI;
        double C = (Math.acos((s3 * s3 - s2 * s2 - s1 * s1) / (-2 * s1 * s2)))
                / PI;
        System.out.println("\n三角形的三个角度为:");
        System.out.printf("A=%1.2fπ B=%1.2fπ C=%1.2fπ", A, B, C);
    }
}
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值