Java - 找不到逻辑错误

时间:2012-09-02 17:05:20

标签: java

所以我有这段代码提示双方的空格分隔输入和三角形的斜边。如果三角形是正确的,则下面的方法应该返回true,否则返回false。

出于某种原因,即使我知道我输入的测量值是直角三角形,它仍会打印出三角形不正确的信息。

我一直试图在这段代码中检测到一个逻辑错误,可以帮忙/打电话给我吗?

import java.util.Scanner;

public class VerifyRightTriangle { public static void main(String[] args) { Scanner sc = new Scanner(System.in);

    System.out.print("Please enter the two sides and the hypotenuse: ");
    String input = sc.nextLine();

    String[] values = input.split(" ");

    int[] nums = new int[values.length];

    for (int i = 0; i < nums.length; i++) {
        nums[i] = Integer.parseInt(values[i]);
    }

    double s1 = nums[0];
    double s2 = nums[1];
    double hyp = nums[2];

    System.out.printf("Side 1: %.2f Side 2: %.2f Hypotenuse: %.2f\n", s1, s2, hyp);

    boolean result = isRightTriangle(s1, s2, hyp);

    if (result == true) {
        System.out.println("The given triangle is a right triangle.");
    } else if (result == false) {
        System.out.println("The given triangle is not a right triangle.");
    }
}

/**
 * Determine if the triangle is a right triangle or not, given the shorter side, the longer
 * side, and the hypotenuse (in that order), using the Pythagorean theorem.
 * @param s1 the shorter side of the triangle
 * @param s2 the longer side of the triangle
 * @param hyp the hypotenuse of the triangle
 * @return true if triangle is right, false if not
 */

private static boolean isRightTriangle(double s1, double s2, double hyp) {
    double leftSide = s1 * s1 + s2 * s2;
    double rightSide = hyp * hyp;
    if (Math.sqrt(leftSide) == Math.sqrt(rightSide)) {
        return true;
    } else {
        return false;
    }
}

}

3 个答案:

答案 0 :(得分:2)

浮点计算不准确,这就是为什么数字永远不匹配,尝试使用BigDecimal代替double

编辑: 在第二个想法,因为你已经解析整数 nums[i] = Integer.parseInt(values[i]);

只需使用int代替double而不使用Math.sqrt,只需使用leftSide==rightside

答案 1 :(得分:1)

您必须在不使用s1*s1 + s2*s2 == hyp*hyp的情况下检查sqrt。请参阅pythagorean theorem

答案 2 :(得分:1)

如果输入为int值,则不应出错。如果我测试

public static void main(String... args) {
    for (int i = 3; i <= 11; i += 2) {
        int side1 = (i * i - 1) / 2;
        int side2 = side1 + 1;
        System.out.println(i + "," + (side1 - 1) + " and " + (side2 - 1) + " is " + isRightTriangle(i, side1 - 1, side2 - 1));
        System.out.println(i + "," + side1 + " and " + side2 + " is " + isRightTriangle(i, side1, side2));
        System.out.println(i + "," + (side1 + 1) + " and " + (side2 + 1) + " is " + isRightTriangle(i, side1 + 1, side2 + 1));
    }
}

打印

3,3 and 4 is false
3,4 and 5 is true
3,5 and 6 is false
5,11 and 12 is false
5,12 and 13 is true
5,13 and 14 is false
7,23 and 24 is false
7,24 and 25 is true
7,25 and 26 is false
9,39 and 40 is false
9,40 and 41 is true
9,41 and 42 is false
11,59 and 60 is false
11,60 and 61 is true
11,61 and 62 is false

这是正确的。

您能提供此方法失败的用例吗?


请尝试此操作以避免出现小错误。

private static boolean isRightTriangle(double s1, double s2, double hyp) {
    double leftSide = s1 * s1 + s2 * s2;
    double rightSide = hyp * hyp;
    return Math.abs(leftSide - rightSide) < 1e-9;
}

如果两个值的sqrt相等,则两个值相等。

Math.sqrt()有一些错误,所以如果你能避免它,你应该。