Java 计算三角形边的长度
我需要编写一个完整的程序,使用以下公式计算三角形边的长度:Java 计算三角形边的长度,java,math,java.util.scanner,Java,Math,Java.util.scanner,我需要编写一个完整的程序,使用以下公式计算三角形边的长度: a^2 = b^2 + c^2 - 2bc cos(degree). 侧面的长度b,c,以及度,由用户指定 这是我的密码: import java.util.*; import static java.lang.Math.*; public class testing { static Scanner console = new Scanner(System.in); public static void main(Str
a^2 = b^2 + c^2 - 2bc cos(degree).
侧面的长度b
,c
,以及度
,由用户指定
这是我的密码:
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
Math.cos()
接受弧度的角度,而不是度。您应该将输入角度转换为弧度
a = sqrt((b*b) + (c*c) - (2*b*c) * cos(toRadians(degree)));
这导致
5.0400416916483275
@HalahAl Mazrou如果此答案解决了您的问题。然后单击答案旁边的右标记接受答案。@Halahl Mazrou如果此答案解决了您的问题。然后单击答案旁边的右标记接受答案。