繁体   English   中英

跳出while循环

[英]Jump out of a while loop

我的while循环有问题。

如果 while 循环条件为false(when the password is wrong) ,则它工作正常,但是当密码正确时,它会同时写入You are logged inWrong password

我明白为什么会这样,但我不明白如何解决这个问题。 我需要使用 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");
    }

}

你应该做的是:

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");
             }
        }
    }
}

在这个过程中,我们有一个无限循环,除非您提供正确的密码,否则它不会中断。 您也可以为此添加一些尝试次数作为破坏条件。

根据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. 您的解决方案应如下所示:

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");
    }
}

您可以使用以下代码。

    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:
    }

}

你可能有一些倒退的事情。 检查错误的密码,而不是循环中的正确密码,然后再次询问他们的密码。 这样,您将收到来自系统的一条消息,这是一个错误的密码,或者在他们“成功登录”后收到一条消息。

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