非常初学者程序员(2周前开始),我目前遇到Math.atan问题。我目前使用的是Eclipse IDE,我一直试图找出如何为一个三角形做tan ^ -1但它目前无法正常工作。我输入10和5进入控制台,但当实际答案是63.43494882时,它出现了0.4636476090008061。我尝试将side1Two和side2Two转换为度数,但它没有用。
import java.util.Scanner;
public class triangleParts {
public static void main(String[] args) {
/*
* |\
* |A\
* | \
* Side 1 | \
* |_ \
* |_|__B\
* Side 2
*/
Scanner side1 = new Scanner(System.in);
System.out.println("Input side 1 here:");
double side1Two = side1.nextDouble();
Scanner side2 = new Scanner (System.in);
System.out.println("Input side 2 here:");
double side2Two = side2.nextDouble();
//Hypotenuse//
double hypotenuse = (Math.sqrt((side1Two * side1Two) + (side2Two * side2Two)));
System.out.println("Hypotenuse");
System.out.println(hypotenuse);
//Angle A//
double angleA = (Math.atan(side2Two/side1Two));
System.out.println("Angle A");
System.out.println(angleA);
//Angle B//
double angleB = (Math.atan(side1Two/side2Two));
System.out.println("Angle B");
System.out.println(angleB);
}
}
答案 0 :(得分:2)
作为提到的Hovercraft Full Of Eels,该方法返回弧度值,而不是度数值。您可以使用以下内置方法将其设置为度:
double getDegrees = Math.toDegrees((Math.atan(side2Two/side1Two)))
答案 1 :(得分:0)
//Angle A//
double angleA = Math.toDegrees((Math.atan(side2Two/side1Two)));
System.out.println("Angle A");
System.out.println(angleA);
//Angle B//
double angleB = Math.toDegrees((Math.atan(side1Two/side2Two)));
System.out.println("Angle B");
System.out.println(angleB);
这,正在发挥作用;)
答案 2 :(得分:0)
以下是您的代码修复为只使用一个扫描仪,并转换为度,因为Math.atan的输出是弧度:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
/*
* |\
* |A\
* | \
* Side 1 | \
* |_ \
* |_|__B\
* Side 2
*/
System.out.println("53000's Simple Java Trigonometry Program");
System.out.println("========================================");
Scanner sc = new Scanner(System.in); //Declare Scanner
System.out.print("Input the length of side 1 here:");
double side1 = sc.nextDouble();
System.out.print("Input the length of side 2 here:");
double side2 = sc.nextDouble();
//Hypotenuse
double hypotenuse = (Math.sqrt((side1 * side1) + (side2 * side2)));
System.out.println("The length of the Hypotenuse is: " + hypotenuse);
//Angle A
double angleA = Math.toDegrees((Math.atan(side2/side1)));
System.out.println("The size of Angle A is: " + angleA);
//Angle B
double angleB = Math.toDegrees((Math.atan(side1/side2)));
System.out.println("The size of Angle B is: " + angleB);
}
}
试试here!