gpt4 book ai didi

Java : Simple Loop

转载 作者:行者123 更新时间:2023-12-02 00:01:52 27 4
gpt4 key购买 nike

这是一个没有任何循环的简单版本。我怎样才能让它像“密码错误。再试一次”一样循环。直到用户输入正确的密码而不是停止程序(或者在停止之前给他们 3 次机会)?

    import java.util.Scanner;
public class helloworld2
{
public static void main(String[] arg)
{
Scanner q = new Scanner(System.in);
long Pass;
boolean auth = true;

System.out.println("Enter Password :");
Pass = q.nextLong();

if ( Pass != 1234 )
{
System.out.println("Wrong Password!");
auth = false;
}
else
{
System.out.println("Password Confirmed.");
auth = true;
}

if (auth) {
////blablabla
}

else
{
return;
}
}
}

最佳答案

public class helloworld2
{
public static void main(String[] arg)
{
Scanner q = new Scanner(System.in);
long Pass;
boolean auth = true;
boolean rightPassword = false;
while(!rightPassword){//repeat until passwort is correct
System.out.println("Enter Password :");
Pass = q.nextLong();

if ( Pass != 1234 )
{
System.out.println("Wrong Password!");
auth = false;
}
else
{
rightPassword = true;//let the loop finish
System.out.println("Password Confirmed.");
auth = true;
}
}
// Here do what you want to do
//because here the user has entered a right password
}
}

关于Java : Simple Loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14692410/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com