计算三角形的边长

computes the length of side of a triangle

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

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

边长bcdegree和用户给定

这是我的代码:

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

程序returns:2.17

我的纸上答案:5.03

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

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

这导致

5.0400416916483275