这就是我所做的,但无论我做什么,我都会不断得到无穷大:
public double calcr(){
double cot = 1 / Math.tan(0);
return .5 * sideLength * cot * (Math.PI / numSides);
}
主要:
RegularPolygon poly = new RegularPolygon(4, 10);
System.out.println(poly.calcr());
输出:
Inifinity
我做错了什么?
最佳答案
问题是你做的
double cot = 1 / Math.tan(0);
这将使 cot
成为 Infinity
。
你想要:
double cot = 1 / Math.tan(Math.PI / numSides);
return .5 * sideLength * cot;
或者,在一行中:
return .5 * sideLength / Math.tan(Math.PI / numSides);
关于java - 如何将这个等式放入java代码中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19285121/