这是两个类的代码,用户必须输入两个角度的值和三角形一部分的长度。我了解 Java 使用弧度,但即使进行简单的转换,使用Math.toDegrees or * 180/Math.Pi
它仍然会得到错误的答案。例如,当输入角度 30 时,100 * tan(30)
度数应为 57.1,但我得到 44.92.... 代码:
/**Class to implement math methods, and math import
*
*/
/**
* @author Oli
*
*/
public class Balloon {
//Attributes
private int angle1;
private int angle2;
private double distance1;
private int time;
private double h2;
//Consttructor
public Balloon (int ang1, int ang2, double dist1, int t1){
angle1 = ang1; angle2 = ang2; distance1 = dist1; time = t1;
}
//Methods to calculate angles, distances and time
public double height1(){
double h1;
h1 = distance1 * (Math.tan(angle1*180/Math.PI));
return h1;
}
public double height2(){
double h2;
h2 = distance1 * (Math.tan(angle2))-(Math.tan(angle1));
return h2;
}
public double speed(){
double sp1;
sp1 = h2/time;
return sp1;
}
public double BalloonDistance(){
double BD1;
BD1 = distance1 / (Math.cos(angle2));
return BD1;
}
}