简体   繁体   中英

How to write while loop condition

The program asks for password and you enter it, else you get 3 attempts and if you fail 3 times it locks (in theory).

Questions:

  1. How should I write the while loop, now it just says "Line can't be resolved to a variable" and I don't know how to solve this.

  2. The subtraction of attempts also doesn't work. How should I write it?

Here's the code:

import java.util.Scanner;

public class Application {
    public static void main(String[] args) {

        while(line != correctPass) {

            String correctPass = "Java";

            System.out.println("Enter the password"); 
            Scanner input = new Scanner(System.in);
            String line = input.nextLine();

            if(line.equals(correctPass)) {
                System.out.println("Wellcome back sir!");
                break;
            }
            else {
                int num = 3;
                System.out.println("Wrong password, please try again! "  + num + " attempts left!");
                num = num - 1;
                    if(num == 0) {
                        System.out.println("System Locked!");
                        break;
                    }
            }
        }
    }
}

line is used before it is declared. Try putting the declaration at the top as String line = null .

Also, because you are setting the num = 3 in the loop, it never gets decremented. It needs to be set to 3 outside of the loop.

I would seriously suggest checking out this tutorial .

There are some other issues too, you may want to check out the difference between something != somethingElse and !something.equals(somethingElse) .

The variables line and correctPass is declared inside the loop, hence the condition statement will not have access to those variables, it should be declared and initialized outside the loop.

It could be something like this:

public class Application {
    public static void main(String[] args) {
        String correctPass = "Java";
        Scanner input = new Scanner(System.in);
        int num = 3;

        System.out.println("Enter the password"); 

        String line;
        while (num != 0 && !(line = input.nextLine()).equals(correctPass)) {

            System.out.println("Wrong password, please try again! "  + num + " attempts left!");
            num = num - 1;
            System.out.println("Enter the password");
        }

        if (num == 0) {
            System.out.println("System Locked!");
        } else {
            System.out.println("Wellcome back sir!");
        }
    }
}

I would prefer to use do while which is the actual logic of do while that you're trying to achieve in a while.

import java.util.Scanner;

public class Application { 

    public static void main(String[] args) {

        String    correctPass = "Java";
        int          attempts = 3;
        boolean authenticated = false;

        Scanner         input = new Scanner(System.in);

        do {
            System.out.println("Enter the password");
            String   userPass = input.nextLine();
            if( userPass.equals(correctPass) ){
                System.out.println("Welcome back sir!");
                authenticated = true;
            } else {
                System.out.println("Wrong password, please try again! "  + attempts + " attempts left!");
                attempts = attempts - 1;
                if(attempts == 0) {
                    System.out.println("System Locked!");
                    break;
                }
            }
        } while(! authenticated); 

        input.close();
    }
}

To point the mistakes in your code,

1) You need to use .equals() to compare a String

2) The original password should be declared before the loop

3) You're re-initialising num variable for every loop (so definitely you won't be locked at any situation ever), so it should also be declared before the loop.

You need to declare line outside the while loop - variables don't exist (and can't be referenced) before they are declared.

You use both correctPass and line in the while loop condition, but they aren't created until 1 and 3 lines later, respectively.

 while(line != correctPass) {
    String correctPass = "Java";

    System.out.println("Enter the password"); 
    Scanner input = new Scanner(System.in);
    String line = input.nextLine();

You need to reformat it to look more like this:

String correctPass = "Java";
int num = 3;
Scanner input = new Scanner(System.in);
System.out.println("Enter the password");
String line = input.nextLine();

while(!line.equals(correctPass)) {
       num = num - 1;
       if(num == 0) {
            System.out.println("System Locked!");
            break;
        }
       System.out.println("Wrong password, please try again! "  + num + " attempts left!");
       line = input.nextLine();
}

Why:

There's no need to recreate correctPass each time the while loop runs, since its the same every time.

Similarly, its silly to recreate the scanner each time, since it isn't changing.

And, as pointed out in comments, if you define num each time you loop it will never reach zero and therefore never lock, since it is redefined as 3 each time.

.equals is needed to compare strings. This is why if you're curious.

  1. 您的while语句后初始化了String变量,因此在评估第一个while条件时该line不存在

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