简体   繁体   中英

Jump out of a while loop

I've a problem with my while loop.

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 .

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.

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

}

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