简体   繁体   中英

JAVA While loop - last else if statement not working

package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
    public static void main(String[] args) {
        System.out.println("What is the password?");
        Scanner new2 = new Scanner(System.in);
        int input = 0;

        while(input <= 5 )
        {
            String password = new2.nextLine();
            if(!password.equals("bluesky123")){
                System.out.println("Incorrect password");
                input++;
            }

            else if("bluesky123".equals(password)) {
                System.out.println("You got it right!");
                break;
            }
            else if(input == 5) {
                System.out.println("maximum number of attempts reached");
                break;
            }
        }    
    }

}

basically, once I hit the 5 loops, it just says "incorrect password" and breaks. not the "maximum attempts" message.

Allow me to annotate:

This if statement will always be evaluated:

        if(!password.equals("bluesky123")){
            System.out.println("Incorrect password");
            input++;
        }

This if statement will only be evaluated if the password is "bluesky123". In this case, it will always evaluate to true .

        else if("bluesky123".equals(password)) {
            System.out.println("You got it right!");
            break;
        }

There is no case when this if statement will ever be evaluated. Once if-else finds a statement that is true, it will skip all others in that section.

        else if(input == 5) {
            System.out.println("maximum number of attempts reached");
            break;
        }

In your case, you should consider a nested if (ie an if inside another if).

I tested your code it is working! The only thing that you need to do is to move the follow line to inside the while loop

 System.out.println("What is the password?");

Doing this it will print "Incorrect password" and then it will print again "What is the password?"

Because in the way that it is working now seems that the software is not waiting the password to be retyped when in fact it is.

while(input <= 5 )
    {
        String password = new2.nextLine();
        if(!password.equals("bluesky123")){
            System.out.println("Incorrect password");
            input++;
        }
        else {
            System.out.println("You got it right!");
            break;
        }

        if((input == 5) && (!password.equals("bluesky123"))) {
            System.out.println("maximum number of attempts reached");
            break;
        }
    }    

Your logic has some flaws. You have to pay attention to how JAVA processes if / else if

https://www.tutorialspoint.com/java/if_else_statement_in_java.htm

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