如何将这个等式放在java代码中?

1 java math equation

这就是我所做的,但无论我持续无限:

 public double calcr(){
  double cot = 1 / Math.tan(0);
  return  .5 * sideLength * cot * (Math.PI / numSides);
}
Run Code Online (Sandbox Code Playgroud)

主要:

RegularPolygon poly = new RegularPolygon(4, 10);   
System.out.println(poly.calcr());
Run Code Online (Sandbox Code Playgroud)

输出:

Inifinity 
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Mih*_*eac 8

问题在于你这样做

double cot = 1 / Math.tan(0);
Run Code Online (Sandbox Code Playgroud)

这将cot成为Infinity.

你想要:

double cot = 1 / Math.tan(Math.PI / numSides);
return .5 * sideLength * cot;
Run Code Online (Sandbox Code Playgroud)

或者,在一行中:

return .5 * sideLength / Math.tan(Math.PI / numSides);
Run Code Online (Sandbox Code Playgroud)