简体   繁体   English

JAVA While 循环 - 最后一个 else if 语句不起作用

[英]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.基本上,一旦我打了 5 个循环,它就会说“密码不正确”并中断。 not the "maximum attempts" message.不是“最大尝试次数”消息。

Allow me to annotate:请允许我注释:

This if statement will always be evaluated:这个 if 语句将始终被评估:

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

This if statement will only be evaluated if the password is "bluesky123".仅当密码为“bluesky123”时才会评估此 if 语句。 In this case, it will always evaluate to true .在这种情况下,它将始终评估为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.永远不会评估此 if 语句的情况。 Once if-else finds a statement that is true, it will skip all others in that section.一旦 if-else 找到一个为真的语句,它将跳过该部分中的所有其他语句。

        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).在您的情况下,您应该考虑嵌套 if(即 if 在另一个 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您唯一需要做的就是将以下行移动到 while 循环内

 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你要注意JAVA如何处理if / else if

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM