简体   繁体   中英

Trouble with nested if-else statements

I am having problems with nesting some if-else statements, at least that is what I think the problem is. I have butchered this code several times trying to figure it out, so I decided to put it back to the original state and ask for some help. A user is asked to input an odd number between 3 and 15. I need to validate that input and if it is not between 3 and 15, output an error message. Same goes if it is not an odd number, it needs to output a different number. When running the code below, if I enter an even number, letter, or number <3 or >15 I get both error messages. If the input is an odd number between 3 and 15, it works fine. I know I have to differentiate between the two errors, just have not been able figure out how to do it and make the code run/work properly. Have also tried else if with no luck. Sure it is something silly, since most of my mistakes seem to be on the silly side. Thanks for your help!

public static void main(String[] args) {

    // Declare variables
    String inputString;
    boolean done = false;
    int numRows = 0;

    // Prompt for user input  
    do
    {
        inputString = JOptionPane
            .showInputDialog("Enter number of rows (odd number between 3 and 15): ");

        // Validating input
        try
        {
            numRows = Integer.parseInt(inputString);
        }
        catch (NumberFormatException e)
        {
            done = true;
        }
        if(numRows % 2 == 0) // Validating input is an odd number
             done = true; 
        if((numRows < 3) || (numRows > 15))// Validating input is between 3 and 15
            done = true;
        else
            done = false;

        if(done)
            JOptionPane.showMessageDialog(null, "Error, not an ODD number. Re-enter odd number between 3 and 15",
                "Error", JOptionPane.ERROR_MESSAGE);    

        if(done)
            JOptionPane.showMessageDialog(null, "Error, number not between 3 and 15. Re-enter odd number between 3 and 15",
                "Error", JOptionPane.ERROR_MESSAGE);
        }
    while(done);
    }

The interesting thing here is, you don't need the if statements. You can greatly simplify (and fix) your boolean expressions.

First, let's establish the behavior of (numRows < 3) || (numRows > 15) (numRows < 3) || (numRows > 15) . For this to be true, either numRows has to be strictly less than 3, or strictly greater than 15. This does not validate a range of numbers - you need to switch this to an AND statement. But even just doing that doesn't validate the correct range - you'll be validating everything that isn't between 3 and 15!

To fix that, you need to flip your inequalities.

You now have (numRows >= 3) && (numRows <= 15) . For this to be true, numRows must be between the bounds of 3 and 15, inclusive.

Now, let's combine that with the check for odd/even, and you arrive at this:

 done = (numRows % 2 == 0) || ((numRows >= 3) && (numRows <= 15));

The behavior of this statement is as such:

  • Either numRows is an even number, OR
  • numRows is bound between 3 and 15, inclusive.

I've also got some thoughts on you combining your JOptionPane statements as well, but that's more of a presentation issue than a code issue - I'll leave that as an exercise for the reader.

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