我應(yīng)該得到 PI 和 PI/2 的余弦和正弦以及角度 0。除了 PI/2 的余弦之外,我的所有數(shù)字都是正確的。預(yù)期輸出:Radians: (cos, sin)0.0: 1.0, 0.01.5707963267948966: 0.0, 1.03.141592653589793: -1.0, 0.0我的輸出:Radians: (cos, sin)0.0: 1.0, 0.01.5707963267948966: 1.0, 1.03.141592653589793: -1.0, 0.0public class UnitCircle { public static void main(String[] args) { System.out.println("Radians: (cos, sin)"); double angle = 0.0; double piDivideTwo = Math.PI/2.0; double pi = Math.PI; System.out.println(angle + ": " + Math.cos(angle) + ", " + Math.sin(angle) ); double cosine = Math.cos(piDivideTwo); cosine = Math.round(cosine * 100) / 100.0; System.out.println(piDivideTwo + ": " + Math.cos(cosine) + ", " + Math.sin(piDivideTwo) ); double sin = Math.sin(pi); sin = Math.round(sin *100) / 100.0; System.out.println(pi + ": " + Math.cos(pi) + ", " + Math.sin(sin) ); }}
1 回答

海綿寶寶撒
TA貢獻1809條經(jīng)驗 獲得超8個贊
為什么它似乎可以使用兩次,cos或者sin只是因為它的價值0
您只需要計算一次cos,sin,然后按格式打印,因為 PI/2 和 PI 不可能是完美值
static void cosSin(double angle) {
double cos = Math.cos(angle);
double sin = Math.sin(angle);
System.out.printf("%.4f : %.1f %.1f \n", angle, cos, sin);
}
public static void main(String[] args) {
System.out.println("Radians: (cos, sin)");
double angle = 0.0;
double piDivideTwo = Math.PI / 2.0;
double pi = Math.PI;
cosSin(angle); // 0,0000 : 1,0 0,0
cosSin(piDivideTwo); // 1,5708 : 0,0 1,0
cosSin(pi); // 3,1416 : -1,0 0,0
}
添加回答
舉報
0/150
提交
取消