我试图根据扫描的点找到三角形的角度/边。
因此,计算边,然后计算角度。我的边长似乎是正确的,但是我一直得到错误的角度值。用于公式的值应该是" height = 3"和" sideA = 3.16。"现在,如果我直接输入这些值,我会得到正确的结果。如果我使用我的高度和sideA变量,即使输出正确的值,我得到的结果也不正确。
输入" 3 / 3.16"的值= .949763 ----------------------------------------------- -------------------------------- 输入值为#34; height / sideA" = .74999
编辑:点值为"(0.0,0.0)(4.0,0.0)(3.0,3.0)"
public class Triangle {
private double sideA;
private double sideB;
private double sideC;
private double height;
private double x1;
private double y1;
private double x2;
private double y2;
private double x3;
private double y3;
public void setX1( double x){
x1 = x;
}
public void setY1( double y){
y1 = y;
}
public void setX2( double x){
x2 = x;
}
public void setY2( double y){
y2 = y;
}
public void setX3( double x){
x3 = x;
}
public void setY3(double y){
y3 = y;
}
public void setHeight(){
height = (x3 - x1);
}
public double getHeight() {
return height;
}
public void setLengthC(){
sideA = Math.sqrt(
Math.pow((x2 - x1), 2.0)
+ Math.pow((y2 - y1), 2.0));
}
public double getLengthC(){
return sideA;
}
public double getAngleA() {
double sine = Math.asin(Math.sin(height/sideA));
return sine;
}
}
这是测试人员类。
import java.util.*;
public class TriangleTester {
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
Triangle test = new Triangle();
System.out.print("Please enter a value for X1: ");
test.setX1(scan.nextDouble());
System.out.print("Please enter a value for Y1: ");
test.setY1(scan.nextDouble());
System.out.print("Please enter a value for X2: ");
test.setX2(scan.nextDouble());
System.out.print("Please enter a value for Y2: ");
test.setY2(scan.nextDouble());
System.out.print("Please enter a value for X3: ");
test.setX3(scan.nextDouble());
System.out.print("Please enter a value for Y3: ");
test.setY3(scan.nextDouble());
test.setLengthC();
test.setHeight();
System.out.println("Side a: " + test.getLengthA());
System.out.println("Side b: " + test.getLengthB());
System.out.println("Side c: " + test.getLengthC());
System.out.println("Height: " + test.getHeight());
System.out.println("Angle A: " + test.getAngleA());
}
}
答案 0 :(得分:0)
在你的setLengthC()函数中,你有sideA = x2和x1的距离。将其更改为x3和x2的距离,以便:
sideA = Math.sqrt(
(Math.pow((x3 - x2), 2.0))
+ (Math.pow((y3 - y2), 2.0)));
}