简体   繁体   English

Java,内部循环并使用扫描程序作为语句

[英]Java, while loop with scanner inside and as a statement

Trying to run the while loop for a password check. 尝试运行while循环以进行密码检查。 I do keep getting an error that the variable haslo cannot be found. 我确实不断收到错误消息,找不到变量haslo。 Tried declaring it outside the loop - then it says, it's already declared. 试图在循环外声明它-然后说,它已经被声明了。 I know it can be done with infinite loop and the break command. 我知道可以通过无限循环和break命令来完成。 Just curious whether it's possible this way. 只是好奇是否有这种可能。

String password = "pw123";

while (!haslo.equals(password)){

        System.out.println("Enter pw:");
        String haslo = reader.nextLine();

        if (haslo.equals(password))
            System.out.println("Right!");

        else
            System.out.println("Wrong!");
}
  1. Declare the variable haslo as String haslo = ""; 将变量haslo声明为String haslo = ""; before the loop starts. 在循环开始之前。
  2. In the loop, replace your line String haslo = reader.nextLine(); 在循环中,替换您的行String haslo = reader.nextLine(); with haslo = reader.nextLine(); haslo = reader.nextLine(); .

Reasoning : 推理

For 1 --> Your while loop is referencing the variable haslo before it is declared. 对于1->,您的while循环在声明变量haslo之前先对其进行引用。 So you need to declare it before it is referenced. 因此,您需要在引用它之前声明它。

For 2 --> Once it is declared, you don't want to re-declare it because the one declared before the loop is already made available within the scope of the loop. 对于2->一旦声明,您就不想重新声明它,因为在循环范围内已经可以使用在循环之前声明的那个。

This is all about scopes. 这都是关于范围的。

Your haslo variable is declared inside the while loop and so can only be used inside it, after he declaration. 您的haslo变量在while循环声明,因此只能在他声明后其中使用。 The condition of the while loop is evaluated before the declaration of haslo so you can't use haslo there. while循环的条件是在声明haslo之前评估的,因此您不能在其中使用haslo

To fix this, declare haslo outside the while loop: 要解决此问题, haslo在while循环外声明haslo

String haslo = "";
while (!haslo.equals(password)){

    System.out.println("Enter pw:");
    haslo = reader.nextLine(); // <-- note that I did not write "String" here because that will declare *another* variable called haslo.

    if (haslo.equals(password))
        System.out.println("Right!");

    else
        System.out.println("Wrong!");
}

Can you try something like this ? 你可以尝试这样的事情吗? You need to declare haslo before the loop, then you can use it inside the loop, and avoid the infinite loop. 您需要在循环之前声明haslo,然后可以在循环内部使用它,并避免无限循环。

String password = "pw123";
String haslo = "";

while (!haslo.equals(password)){

    System.out.println("Enter pw:");
    haslo = reader.nextLine();

    if (haslo.equals(password))
        System.out.println("Right!");

    else
        System.out.println("Wrong!");

}

The problem can be found by following it step by step: 可以通过逐步解决该问题来发现问题:

String password = "pw123"; (Good)

while (!haslo.equals(password)){ (Bad. What is Haslo? It is not defined before you use it here)

Try this: 尝试这个:

String password = "pw123";
String haslo = "";
while (!haslo.equals(password)){
    System.out.println("Enter pw:");
    haslo = reader.nextLine();

    if (haslo.equals(password))
        System.out.println("Right!");

    else
        System.out.println("Wrong!");

}

Declare String INSIDE loop again! 再次声明字符串INSIDE循环! otherwise next time loop will not work! 否则下一次循环将不起作用! you gave it a reference, but loop needs to know what the reference is again. 您给了它一个参考,但是循环需要再次知道该参考是什么。

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

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