繁体   English   中英

为什么我的密码do-while循环在Java中不起作用? 我做错了什么?

[英]Why won't my password do-while loop in Java won't Work? What did I do wrong?

我遇到了do-while循环的问题。 它里面有两个if语句。 该程序应该使用您的用户名和密码(您输入),然后再次输入它们来确认它们。 再次键入时,必须与第一次键入时相同。 当布尔重做设置为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!"); 
        }
    }
while(redo = true); 

这是一项任务而不是比较。 这将永远是true

while(redo == true);

是你打算键入的,但是

while(redo);

是你真正想要的,因为它使得无法提交赋值 - 而不是比较错误。

比较除boolean和变量之外的常量时,最好将常量放在第一位,原因相同。

if (1 == someInt)

代替

if (someInt == 1)

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

while(redo = true) 

结果始终为true因为它等于while(true)

=是作业

==是比较。

暂无
暂无

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

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