简体   繁体   中英

Java - program not reading 'else' properly? (logic error)

I have been working on this program for determining if a right triangle can be formed by three user-inputted side lengths. My program uses the Pythagorean Theorem , being a2 + b2 = c2 . I can get the program to recongnise when a triangle CANNOT be built, but I can't seem to get it to recognise when it can be built.

Any suggestions? I am unsure if this is an error with my if statements, or a simple logic error. (I am a total beginner at coding, so I apologise for the simplicity of this question).

String myInputA = JOptionPane.showInputDialog(null, "Hello, and welcome to the 'Right' Triangle Tester.\nThis program will determine if three side lengths form a right triangle. \nPlease input the first side length below.","Right Triangle Tester",JOptionPane.INFORMATION_MESSAGE);
    String myInputB = JOptionPane.showInputDialog(null, "Great. Please enter the second side below.","Right Triangle Tester", JOptionPane.INFORMATION_MESSAGE);
    String myInputC = JOptionPane.showInputDialog(null, "Please enter the last side below.","Right Triangle Tester", JOptionPane.INFORMATION_MESSAGE);
    double sideA = Double.parseDouble(myInputA);
    double sideB = Double.parseDouble(myInputB);
    double sideC = Double.parseDouble(myInputC);
    if ((sideA * sideA) != ((sideB * sideB) + (sideC * sideC)))
    {
JOptionPane.showMessageDialog(null,"I am sorry. Those side lengths do not form a right triangle.","Right Triangle Tester", JOptionPane.ERROR_MESSAGE);
    }
    else if ((sideB * sideB) != (sideC * sideC) + (sideA * sideA))
    {
JOptionPane.showMessageDialog(null,"I am sorry. Those side lengths do not form a right triangle.","Right Triangle Tester", JOptionPane.ERROR_MESSAGE);
    }
    else if ((sideC * sideC) != (sideA * sideA) + (sideB * sideB))
    {
        JOptionPane.showMessageDialog(null,"I am sorry. Those side lengths do not form a right triangle.","Right Triangle Tester", JOptionPane.ERROR_MESSAGE);
    }
        else 
    {
    JOptionPane.showMessageDialog(null, "Congratulations, those side lengths form a right triangle.","Right Triangle Tester", JOptionPane. INFORMATION_MESSAGE);
}

Your logic is wrong. If at least one value^2 is the value of the other two, it's a triangle.

In your logic, you'll get to the else part only if all three values are sum of the other two (which can only happen if all are 0)

Change your condition checking to be like below. Check for right angle condition in if block; else show the error message.

bool IssideA = (sideA * sideA) == ((sideB * sideB) + (sideC * sideC));
bool IssideB = (sideB * sideB) == (sideC * sideC) + (sideA * sideA);
bool IssideC = (sideC * sideC) == (sideA * sideA) + (sideB * sideB);

    if (IssideA || IssideB || IssideC)
    {
JOptionPane.showMessageDialog(null, "Congratulations, those side lengths form a right triangle.","Right Triangle Tester", JOptionPane. INFORMATION_MESSAGE);
    }
    else 
    {
        JOptionPane.showMessageDialog(null,"I am sorry. Those side lengths do not form a right triangle.","Right Triangle Tester", JOptionPane.ERROR_MESSAGE);    
    }

Mathematically, if a 2 + b 2 = c 2 , then c must be larger than a or b. If you determine which length is the hypotenuse, your logic will simplify, because if a 2 + b 2 = c 2 , then a 2 + b 2 + c 2 = 2 * c 2 ; you don't actually have to know which is which.

Given:

double a,b,c;

To determine if the triangle is a right angle, find the hypotenuse and compare the sum of squares with twice the hypotenuse squared:

double h = a > b ? (a > c ? a : c) : (b > c ? b : c); 
if (h * h * 2 == a * a + b * b + c * c)
    // yes!

Note that you may fall foul of the imprecision of the double type. I would recommend converting this code to us BigDecimal to avoid this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM