为什么编译器无法识别Math.PI()?

时间:2017-11-01 02:09:00

标签: java compiler-errors

更新:代码中的新问题(见下文)
对于Java作业,我们需要计算单位圆的pi / 4的每个间隔的cos和sin值并将其打印出来。但是,虽然我使用了舍入技术,但使用了舍入技术,使用了double doubleName和doubleName = Math.round(doubleName * 100)/ 100.0。

结果应该是:
Radians :( cos,sin)
0.0:1.0,0.0
0.79:0.7,0.71
1.57:0.0,1.0
2.36:-0.71,0.7
3.14:-1.0,0.0
3.93:-0.7,-0.71
4.71:0.0,-1.0
5.5:0.71,-0.71

但我的输出是:
Radians :( cos,sin)
0.0:1.0,0.0
0.79:0.7,0.71
1.58:-0.01,1.0
2.37:-0.72,0.7
3.16:-1.0,-0.02
3.95:-0.69,-0.72
4.74:0.03,-1.0
5.53:0.73,-0.68

我的代码:

public class UnitCircle extends ConsoleProgram {
  public void run() {
    System.out.println("Radians: (cos, sin)");
    for (double i = 0; i <= 2 * Math.PI; i += Math.PI / 4) {
      i = Math.round(i * 100.0) / 100.0;
      double x = Math.cos(i);
      x = Math.round(x * 100.0) / 100.0;
      double y = Math.sin(i);
      y = Math.round(y * 100.0) / 100.0;
      System.out.println(i + ": " + x + ", " + y);
    }

  }
}

1 个答案:

答案 0 :(得分:2)

您正在使用Math.PI()。查看此处的Java Math Class文档:https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html

没有PI方法,但有一个PI Field。所以试试:

Math.PI