Home > OS >  I am making a program where you ask use to input three numbers and telling the user if the numbers t
I am making a program where you ask use to input three numbers and telling the user if the numbers t

Time:06-20

I made a program that asks a user to input three numbers and then tells the user if the numbers they entered could make a triangle and what kind of triangle it would be. Each number they enter represents one side of the triangle. I get it to pass all of my test cases except the ones where I enter sides that make the triangle a scalene triangle. The else statement that is supposed to identify the scalene triangles doesn't output and I don't know why.

public static void main(String[] args) {
    System.out.println("Hello, my name is Christian");
    System.out.println("In this program you will enter three whole numbers and\nI will tell you if the numbers you entered equal a triange.");
    
    
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter your first whole number:");
    int sideA = Integer.parseInt(scan.nextLine());
    
    
    System.out.println("Please enter your second whole number:");
    int sideB = Integer.parseInt(scan.nextLine());
    
    
    System.out.println("Please enter your third whole number:");
    int sideC = Integer.parseInt(scan.nextLine());
    
    // This if statement pulls in any combination of integers for evaluation. 
    if (((sideA   sideB) > sideC)||((sideB   sideA) > sideA)||((sideA   sideC) > sideB)){
        
        if (sideA == sideB && sideA == sideC) {
            System.out.println("That is an equilateral triangle.");
        } 
        //this statement identifies any combination of integers that are not triangles.
        if (((sideA   sideB) <= sideC)||((sideB   sideA) <= sideA)||((sideA   sideC) <= sideB)) {
            System.out.println("That is not a valid triangle.");
        }
        
        if (sideA == sideB && (sideA   sideB) > sideC) {
            System.out.println("That is an isosceles triangle.");
        }
        
        if (sideB == sideC && (sideB   sideC) > sideA) {
            System.out.println("That is an isosceles triangle.");
        }
        
        if (sideC == sideA && (sideC   sideA) > sideB) {
            System.out.println("That is an isosceles triangle.");
        
        }


    } else {
        System.out.println("That is a scalene triangle.");
    }

}

}

CodePudding user response:

You basically need the following structure (pseudo-code):

if (is valid) {
    if (is equilateral) {
        print equilateral;
    } else if (is isosceles) {
        print isosceles;
    } else {
        print scalene;
    }
} else {
    print not valid;
}

or, slightly changed - as I would prefer, since its structure better shows that only ONE output is to be chosen:

if (is not valid) {
    print not valid;
} else if (is equilateral) {
    print equilateral;
} else if (is isosceles) {
    print isosceles;
} else {
    print scalene;
}

CodePudding user response:

The first if statement is almost always correct, and it is likely that most of your cases satisfy at least one of the conditions. For instance, "(sideB sideA) <= sideA" is correct for any two non-negative numbers, so inputting sideA and SideB as any non-negative integers will result in the first if statement being correct and therefore not entering the else statement.

A better approach would be to write it the following way (I use "else if" if you want only one line of output), I also added a check to ensure the integers are positive:

    // this statement identifies any combination of integers that are not triangles.
    if (((sideA   sideB) <= sideC) || ((sideB   sideC) <= sideA) || ((sideA   sideC) <= sideB) || sideA <=0 || sideB <=0 || sideC <=0) {
        System.out.println("That is not a valid triangle.");
    }
    // this statement identifies any combination of integers that are equilateral triangles.
    else if (sideA == sideB && sideA == sideC) {
        System.out.println("That is an equilateral triangle.");
    }
    // this statement identifies any combination of integers that are isosceles triangles.
    else if (sideA == sideB || sideB == sideC || sideA == sideC) {
        System.out.println("That is an isosceles triangle.");
    }
    // this catches any scalene triangles.
    else{
        System.out.println("That is a scalene triangle.");
    }

CodePudding user response:

You have no output because your outer if statement is wrong. First of all, you don't need that many brackets - if you remove them, you might find an error, although I'm not sure what you're trying to check for here:

if (sideA   sideB > sideC || sideB   sideA > sideA || sideA   sideC > sideB)

I think what you meant to write here was (in the second part of the condition) was B C > A, and also you'd need AND instead of OR between these conditions if you'd like to validate the arguments (thus the inner "this is not a valid triangle" statement would be redundant):

if (sideA   sideB > sideC && sideB   sideC > sideA && sideA   sideC > sideB)

Then I'd also suggest to use if-else if-else inside it, as in its current state the program evaluates all if statements, despite finding an answer.

The scalene triangle would be the last "else" block, and the outer else block (which now contains the scalene triangle output) would be the "invalid triangle", somehow like this (you can also merge the three isosceles statements into one):

if (sideA   sideB > sideC && sideB   sideC > sideA && sideA   sideC > sideB) {
    if (sideA == sideB && sideA == sideC) {
        System.out.println("That is an equilateral triangle.");
    } else if (
      sideA == sideB && (sideA   sideB) > sideC
        || sideB == sideC && (sideB   sideC) > sideA
        || sideC == sideA && (sideC   sideA) > sideB
    ) {
        System.out.println("That is an isosceles triangle.");
    } else {
        System.out.println("That is a scalene triangle.");
    }
} else {
    System.out.println("That is not a valid triangle.");
}
  • Related