【发布时间】:2016-06-25 04:18:30
【问题描述】:
我需要这个 java 练习的帮助。说明是
编写一个程序,使用 Math.sin() 和 Math.cos() 来检查 对于任何输入的 θ,sin2θ + cos2θ 的值大约为 1 命令行参数。只需打印值。为什么价值观不 总是正好 1?
到目前为止我的代码(这是我的第一个代码,所以请不要苛刻的判断)。
public class math {
public static void main(String[] args) {
//Changes the data types from string to double
double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
//Does the calculation math functions
double s = 2*Math.sin(a);
double c = 2*Math.cos(b);
double target = .9998; // var for comparing if less than 1
double answer = s + c;
// Prints the answer and checks whether its less than 1
System.out.println(answer < target);
System.out.println(answer);
}
}
我猜想将 sin 和 cos 平方。如果有人对如何平方 sin 和 cos 有快速建议,如果我的小程序有错误,我将感谢您的帮助。
【问题讨论】:
-
您将 sin 和 cos 乘以 2。如果你想求平方,你应该使用 Math.pow。更简单的方法是将 sin/cos 与自身相乘。
-
此外,您应该使用 Math.toRadians() 方法将输入转换为弧度,因为数学三角函数使用弧度值作为参数。