简体   繁体   English

跳出while循环

[英]Jump out of a while loop

I've a problem with my while loop.我的while循环有问题。

It works fine if the while loop conditions is false(when the password is wrong) , but when the password is right it writes both You are logged in and Wrong password .如果 while 循环条件为false(when the password is wrong) ,则它工作正常,但是当密码正确时,它会同时写入You are logged inWrong password

I understand why it does so but I don't understand how to solve the problem.我明白为什么会这样,但我不明白如何解决这个问题。 I need to use a while loop because it's a school assignment that requires it.我需要使用 while 循环,因为这是需要它的学校作业。

import java.util.Scanner;
public class Password {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        String rightPassword = "Hello123";

        System.out.println("Write your password: ");
        String scan = scanner.nextLine();

        while(scan.equals(rightPassword)){
            System.out.println("You are logged in");
            break;
        }
        System.out.println("Wrong password");
    }

}

What you should have done is :你应该做的是:

import java.util.Scanner;
public class Password {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String rightPassword = "Hello123";
        String scan="";
        while(true){
             System.out.println("Write your password: ");
             scan = scanner.nextLine();
             if(scan.equals(rightPassword)) {
                 System.out.println("You are logged in");
                 break;         
             } else {
                 System.out.println("Wrong password");
             }
        }
    }
}

In this what we do is we have an endless for loop which will not break until you supply the right password.在这个过程中,我们有一个无限循环,除非您提供正确的密码,否则它不会中断。 You can add some kin of number of tries to this as a breaking condition too.您也可以为此添加一些尝试次数作为破坏条件。

Based on the comment I know, it become a infinite loop and that's why i use break. My requirement is that i must use a While loop. I know how to use a if-loop to solve the problem.根据I know, it become a infinite loop and that's why i use break. My requirement is that i must use a While loop. I know how to use a if-loop to solve the problem.的评论I know, it become a infinite loop and that's why i use break. My requirement is that i must use a While loop. I know how to use a if-loop to solve the problem. I know, it become a infinite loop and that's why i use break. My requirement is that i must use a While loop. I know how to use a if-loop to solve the problem. Your solution should look like :您的解决方案应如下所示:

import java.util.Scanner;
public class Password {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String rightPassword = "Hello123";
        System.out.println("Write your password: ");
        while(!rightPassword.equals(scanner.nextLine())) {
            System.out.println("Wrong password");
            System.out.println("Write your password: ");
        }
        System.out.println("You are logged in");
    }
}

You can use below code.您可以使用以下代码。

    int a=1;
    Scanner scanner = new Scanner(System.in);

    String rightPassword = "Hello123";

    System.out.println("Write your password: ");
    String scan = scanner.nextLine();

    while(scan.equals(rightPassword)){
        System.out.println("You are logged in");
        a=0;
       break;
    }
    if(a==1){
        System.out.println("Wrong password");
    }
without changing your code this will help you.


import java.util.Scanner;
public class Password {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        String rightPassword = "Hello123";

        System.out.println("Write your password: ");
        String scan = scanner.nextLine();

        while(scan.equals(rightPassword)){
            System.out.println("You are logged in");
            break end;
        }
        System.out.println("Wrong password");
        end:
    }

}

You may have a couple of things backwards.你可能有一些倒退的事情。 Check for a wrong password, not a right one in the loop, and ask for their password again.检查错误的密码,而不是循环中的正确密码,然后再次询问他们的密码。 This way you will receive one message from the system and that's a bad password, or a good one once they 'successfully log in'.这样,您将收到来自系统的一条消息,这是一个错误的密码,或者在他们“成功登录”后收到一条消息。

import java.util.Scanner;
public class Password {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        String rightPassword = "Hello123";

        System.out.println("Write your password: ");
        String scan = scanner.nextLine();

        while(!scan.equals(rightPassword)){
            System.out.println("Wrong password. Please try again");
            System.out.println("Write your password: ");
            String scan = scanner.nextLine();
        }
        System.out.println("You are logged in");
    }

}

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

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