mercredi 27 mars 2019

If statements not working correctly in program to determine triangle type

I have an assignment to write a program that uses the method triangleType that i must write to take three int inputs from the user and output the triangle type. In the method, I first need to sort the integers in ascending order so that the comparisons I am required to use will work correctly. I know I did the sorting part in the code correctly, because I tested it before I even started trying to determine the triangle type. I am required to use these comparisons to find the triangle type: "if A + B <= C, then the sides do not represent a valid triangle. if A = C (all the sides must be the same length) then the triangle is EQUILATERAL. if A = B or B = C, then the triangle is ISOSCELES; otherwise the triangle is SCALENE" For some reasons my if statements at the end of the triangleType method are not working correctly and I get all sorts of output, including "Invalid Triangle" plus other outputs no matter the integers that I enter.

package trianglemethod;

import javax.swing.JOptionPane;


public class TriangleMethod
{


    public static void main(String[] args)
    {
        String wordaA, wordbB, wordcC, answer;

        do 
        {
           System.out.println("Please enter all 3 side lengths of the triangle in any order.");
           wordaA = JOptionPane.showInputDialog("Enter side 1:");
           wordbB = JOptionPane.showInputDialog("Enter side 2:");
           wordcC = JOptionPane.showInputDialog("Enter side 3:");
           int aA = Integer.parseInt(wordaA);
           int bB = Integer.parseInt(wordbB);
           int cC = Integer.parseInt(wordcC);
           triangleType(aA,bB,cC);
           System.out.println("Would you like to enter another triangle?");
           answer = JOptionPane.showInputDialog("Would you like to enter another triangle?");
        } while (answer.equalsIgnoreCase("yes"));

    }

    static void triangleType(int aA, int bB, int cC) {
        int a=0, b=0, c=0;

        if (aA > bB && aA > cC)
        {
            if (bB > cC)
            {
              a = cC;
              b = bB;
              c = aA;
            }
            else if (cC > bB)
            {
              a = bB;
              b = cC;
              c = aA;
            }
        }
        if (bB > aA && bB > cC)
        {
            if (aA > cC)
            {
              a = cC;
              b = aA;
              c = bB;
            }
            else if (cC > aA)
            {
              a = aA;
              b = cC;
              c = bB;
            }
        }
        if (cC > aA && cC > bB)
        {
            if (aA > bB)
            {
              a = bB;
              b = aA;
              c = cC;
            }
            else if (bB > aA)
            {
              a = aA;
              b = bB;
              c = cC;
            }
        }

        if (a+b<=c)
        {
            JOptionPane.showMessageDialog(null,"Invalid Triangle");
        } if (a==c) {
               JOptionPane.showMessageDialog(null,"Triangle is Equilateral"); 
        } if (a==b || b==c){
                JOptionPane.showMessageDialog(null, "Triangle is Isosceles");
        } 
        else {
            JOptionPane.showMessageDialog(null,"Triangle is Scalene");
        }    
    }
}

Aucun commentaire:

Enregistrer un commentaire