0

所以我有这段代码提示输入两侧的空格分隔输入和三角形的斜边。如果三角形是正确的,则 main 下面的方法应该返回 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;
    }
}

}

4

3 回答 3

1

浮点计算不精确,这就是数字永远不匹配的原因,请尝试使用BigDecimal而不是double

编辑:重新考虑,因为您已经在解析整数 nums[i] = Integer.parseInt(values[i]);

简单地使用int代替double和不使用Math.sqrt,简单地使用leftSide==rightside

于 2012-09-02T17:09:32.337 回答
1

您必须在s1*s1 + s2*s2 == hyp*hyp不使用的情况下进行检查sqrt。见勾股定理

于 2012-09-02T17:12:40.713 回答
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() 有一些错误,所以如果你能避免它,你应该这样做。

于 2012-09-02T17:16:22.103 回答