gpt4 book ai didi

java - 计算三角形的边长

转载 作者:行者123 更新时间:2023-12-01 18:10:04 25 4
gpt4 key购买 nike

我需要编写一个完整的程序,使用以下公式计算三角形的边长:

 a^2 = b^2 + c^2 - 2bc cos(degree).

边长bc由用户给出。

这是我的代码:

import java.util.*; 
import static java.lang.Math.*;
public class testing {
static Scanner console = new Scanner(System.in);
public static void main(String args[]) {

double a, b, c, degree;

System.out.println ("Enter B, C and degree");
b = console.nextDouble();
c = console.nextDouble();
degree = console.nextDouble();

a = sqrt(((b*b) + (c*c) - (2*b*c))*cos(degree));

System.out.printf ("answer %.2f ",a);
}
}

但是,答案与我在纸上解决的答案不同。如果:

b = 4,  c= 7,  degree = 45

程序返回:2.17

我的纸上答案:5.03

最佳答案

Math.cos() 接受以弧度为单位的角度,而不是度数。您应该将输入角度转换为弧度。

a = sqrt((b*b) + (c*c) - (2*b*c) * cos(toRadians(degree)));

这会导致

5.0400416916483275

关于java - 计算三角形的边长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33708741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com