我不知道我是否在我的代码中做错了什么,我正在测试我的三角形类,并且由于某种原因,我的三角形构造函数的角 C 将 x 与 y 切换。
public class TriangleIsosceles {
private Point cornerA;
private Point cornerB;
private Point cornerC;
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public TriangleIsosceles(){
cornerA = new Point(x1,y1);
cornerB = new Point(x2,y2);
cornerC = new Point(x3,y3);
}
public TriangleIsosceles(int X1,int Y1,int X2,int Y2,int X3,int Y3){
x1 = X1;
y1 = Y1;
x2 = X2;
y2 = Y2;
x3 = X3;
y3 = Y3;
cornerA = new Point(X1,Y1);
cornerB = new Point(X2,Y2);
cornerC = new Point(X3,Y3);
}
public boolean isIsosceles(){
double lengthAB = Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2));
double lengthBC = Math.sqrt(Math.pow(x2-x3,2) + Math.pow(y2-y3,2));
double lengthCA = Math.sqrt(Math.pow(x3-x1,2) + Math.pow(y3-y1,2));
boolean isIsosceles = false;
if(lengthAB == lengthBC || lengthBC == lengthCA || lengthCA == lengthAB){
isIsosceles = true;
}
System.out.println(lengthAB);
System.out.println(lengthBC);
System.out.println(lengthCA);
return isIsosceles;
}
}
在我的测试器类中,我尝试
TriangleIsosceles t2 = new TriangleIsosceles(0, 0, 0, 10, 0, 5);
System.out.println(t2.isIsosceles());
了输出为
10.0
5.0
5.0
true
但是当我尝试
TriangleIsosceles t2 = new TriangleIsosceles(0, 0, 0, 10, 5, 0);
System.out.println(t2.isIsosceles());
输出为
10.0
10.295630140987
5.0990195135927845
假