【问题标题】:Why won't my password do-while loop in Java won't Work? What did I do wrong?为什么我的密码 do-while 循环在 Java 中不起作用?我做错什么了?
【发布时间】:2014-04-01 21:17:55
【问题描述】:

我遇到了 do-while 循环的问题。它里面有两个 if 语句。该程序应该获取您的用户名和密码(您输入),然后通过再次输入它们来确认它们。当您再次键入它们时,它们必须与您第一次键入它们时相同。当 boolean redo 设置为 false 时,do-while 循环应该停止(当您正确地重新输入用户名和密码时,它会设置为 false)但是循环继续进行,即使它说您获得了用户名和密码正确。 (它说欢迎,(用户名))然后循环再次进行并要求您重新输入您的用户名和密码。获得正确密码后如何停止此循环?请帮忙。

package Pack1; 
import java.util.Scanner; 
public class class1 { 
    public static void main(String[] args){

        String Username; //Used to set the original username
        String Password; //Used to set the original password
        String Usernameuse; //Used as a test. This one has to be equal to the original username.
        String Passworduse; //Used as a test. This one has to be equal to the original password.
        boolean redo; //This is to determine whether the do-while loop will repeat.

        Scanner in1 = new Scanner(System.in);  //getting the original username
        System.out.println("Enter your desired username");
        Username = in1.nextLine(); 

        Scanner in2 = new Scanner(System.in); //getting original password
        System.out.println("Enter your desired password");
        Password = in2.nextLine(); 

        System.out.println("Identity Confirmation-- Enter your account information");

        do{
        Scanner in3 = new Scanner(System.in); //gets second username which has to be equal to original
        System.out.println("Please Enter your Username");
        Usernameuse = in3.nextLine(); 

        Scanner in4 = new Scanner(System.in); //gets second password which has to be equal to the original
        System.out.println("Please Enter your Password");
        Passworduse = in4.nextLine();

        if(Usernameuse.equals(Username) && Passworduse.equals(Password)){ //determines if both are true
            System.out.println("Welcome, " + Username);
            redo = false; //makes redo = false
        }
        if(!Usernameuse.equals(Username) || !Passworduse.equals(Password)){ //determines if either one is false
            System.out.println("Either Username or Password are incorrect, please redo");
            redo = true; //makes redo = true
        }

        } while(redo = true); //Is supposed to stop looping when you set redo to false, by entering correct username and password
        System.out.println("You are now on your secret account!"); 
        }
    }

【问题讨论】:

  • 不要为每个输入行创建一个新的 Scanner。您必须重复使用一个扫描仪。

标签: java loops while-loop do-while


【解决方案1】:
while(redo = true); 

这是一个赋值而不是比较。这将永远是true

while(redo == true);

是你要输入的,但是

while(redo);

是你真正想要的,因为它使得不可能提交 assignment-instead-of-comparison 错误。

当您比较 boolean 以外的常量和变量时,出于同样的原因,最好将常量放在首位。

if (1 == someInt)

而不是

if (someInt == 1)

如果您不小心使用了= 而不是==,则常量优先表单将无法编译。

【讨论】:

  • 你的帮助奏效了,谢谢你,感谢所有说要写 while(redo);
  • 很高兴为您提供帮助,有机会请将此标记为答案!
【解决方案2】:
while(redo = true) 

结果总是true,因为它等于while(true)

= 是赋值

== 是比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    相关资源
    最近更新 更多