Java JUnit测试以检查三角形是否为矩形
我们有一个接口RtriangleJava JUnit测试以检查三角形是否为矩形,java,unit-testing,geometry,Java,Unit Testing,Geometry,我们有一个接口Rtriangle public interface Rtriangle { int getApexX1(); int getApexY1(); int getApexX2(); int getApexY2(); int getApexX3(); int getApexY3(); } 有一节课 public final class RtriangleProvider { public static Rtriangle getTriangle() {
public interface Rtriangle {
int getApexX1();
int getApexY1();
int getApexX2();
int getApexY2();
int getApexX3();
int getApexY3();
}
有一节课
public final class RtriangleProvider {
public static Rtriangle getTriangle() {
return new Rtriangle() {
@Override
public int getApexY3() {
return 1;
}
@Override
public int getApexY2() {
return 1;
}
@Override
public int getApexY1() {
return 1;
}
@Override
public int getApexX3() {
return 1;
}
@Override
public int getApexX2() {
return 1;
}
@Override
public int getApexX1() {
return 1;
}
};
}
}
需要编写一个测试来检查矩形三角形与否。如果不是,则应输出一个错误。
我编写了一个示例测试,但事实并非如此
public class TriangleTest {
@Test
public void testGetTriangle() {
Rtriangle rt = RtriangleProvider.getTriangle();
int x1 = rt.getApexX1();
int x2 = rt.getApexX2();
int x3 = rt.getApexX3();
int y1 = rt.getApexY1();
int y2 = rt.getApexY2();
int y3 = rt.getApexY3();
int side1 = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
int side2 = (x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2);
int side3 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
boolean hypo1 = (side1 == (side2 + side3));
boolean hypo2 = (side2 == (side1 + side3));
boolean hypo3 = (side3 == (side1 + side2));
boolean result = hypo1 || hypo2 || hypo3;
if (!result) {
throw new AssertionError();
}
}
}
也许我没有正确地计算派对。
请帮忙。您可以在这里检查您的三角形是否满足要求 和你在这里做的一样 您应该能够创建不同的点(3组坐标)。然后你可以用这个逻辑 在您的情况下,所有三点都是相同的
(1,1)
。这不是一个三角形。你可以试试下面的方法
现在你必须聪明点
boolean result = hypo1 || hypo2 || hypo3; // by pythagoras theorem only one
// should true
不可能所有的都是true
改变
boolean result = hypo1 || hypo2 || hypo3;
到
为什么?
这是不正确的。你必须把这个案子作为false是的,我理解。坐标可以是任意的。但这里的问题是,由于某种原因,这个测试通过了。他没有在不忠方面犯错误。@Vasily你可以试试我的建议。我不这么认为……布尔结果=hypo1 | | hypo2 | hypo3;如果a2+c2=b2-->hypo2-->为真,则a2+b2=c2-->hypo1-->为真,依此类推。如果一个hypo为真,则表示它是矩形三角形。@PiumiWandana不是真的<当所有三个变量均为
true
时,code>result=hypo1 | | hypo2 | | hypo3将为您提供true
。这种情况是直角三角形吗?我想我找到了正确的解决方案assertTrue((side1+side2>side3)&&(side2+side3>side1)&(side3+side1>side2))_
boolean result=false;
boolean[] all = { hypo1, hypo2,hypo3 };
boolean result=false;
for (boolean a : all) {
for (boolean b: all) {
for(boolean c:all){
result =(a^b^c) && !(a&&b&&c);
}
}
}
boolean result = hypo1 || hypo2 || hypo3;// this will give you true
// for all 3 variables are true