简体   繁体   中英

How to write a nested if statement within a Do-While Loop?

I am a beginner Java student. This is the general idea of what I'm doing.

I have a list of things that a user can pick from by inputting the corresponding number. After they input ONE integer, the string next to the item prints as YES . If they decide they don't want it anymore, they have to input that same number again and then string is supposed to change to NO . My nested loop technique is allowing for this change, but changes it right back after reading the next if statement. I have been working on this for a very long time. Can anyone please nudge me in the right direction to identify this problem?

     do
    {
        int num=input.nextInt();  

        if (num == 7)
        {               
            if(s.equals("NO"))  //corresponding string
            {
               s = "YES";
            }
            if(s.equals("YES"))  //same corresponding string
            {
               s = "NO";
            }
        }

    //similar if statements for different conditions 
    //similar if statements for different conditions 


    }while(myBoolean()==true);

You seem to be missing an else statement.

if(s.equals("NO"))  //corresponding string
{
   s = "YES";
} else if(s.equals("YES"))  //same corresponding string
{
   s = "NO";
}

or if you want to shorten things a bit:

s = s.equalsIgnoreCase("NO") ? "YES" : "NO";

What you should do is use an else if statement like so

if (num == 7)
{               
    if(s.equals("NO"))
    {
       s = "YES";
    }
    else if(s.equals("YES"))
    {
       s = "NO";
    }
}

If the first if statement is true , it will skip the else if statement. If the if statement is false , it will read the else if statement. You can also have multiple else if statements like so

if(boolean)
{
   ....
}
else if(another boolean)
{
   ....
}
else if(some other boolean)
{
   ....
}

If the if statement and all else if statements are false , you can add an else statement, which will be read

if(boolean)
{
   ....
}
else if(another boolean)
{
   ....
}
else
{
   ....
}
 do
{

Just add the word "else" in between your "if" statements. In your example, when s is "NO", you change it to "YES". Therefore s is "YES" when you hit your second "if" statement.

Better yet, instead of testing for both values "YES and "NO", just test for one of them and assume the opposite case if the test fails.

Eg: do { int num=input.nextInt();

    if (num == 7)
    {               
        if(s.equals("NO"))  //corresponding string
        {
           s = "YES";
        }
        else  // <--- This is the only change I made.
        {
           s = "NO";
        }
    }

//similar if statements for different conditions 
//similar if statements for different conditions 


}while(myBoolean()==true);

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