gpt4 book ai didi

java - 如何发起重试 for 循环

转载 作者:行者123 更新时间:2023-11-30 08:02:07 25 4
gpt4 key购买 nike

初学者,我正在创建一个非常简单的“登录”样式程序,目前我使用 if 语句来查看用户输入的内容是否与我已经声明的内容相匹配,如果他们正确,他们会得到一个“已登录”消息,如果没有则程序终止。

但是,我希望用户在终止之前最多可以尝试 3 次来获取正确的凭据。我该如何去做呢?

List<String> usernames = accessClass.setUsernames();
List<String> passwords = accessClass.setPasswords();

System.out.print("Please enter your username: ");

user1 = wordScan.next();


if(usernames.contains(user1)){

System.out.print("Now enter your password: ");

pass1=wordScan.next();

if(passwords.contains(pass1)){

System.out.println("Logged in.");
System.out.println("Your username is "+user1);

random.setSeed(System.currentTimeMillis());
numGen = random.nextInt(899999)+100000;

System.out.println("Your access code is: "+numGen);
}
}else{

System.out.println("Invalid Username... Terminating.");
System.exit(0);
}

wordScan.close();

}

非常感谢

最佳答案

我也会使用这样的循环:

    List<String> usernames = accessClass.setUsernames();
List<String> passwords = accessClass.setPasswords();

while(attempts > 0 && !loggedIn) {

System.out.print("Please enter your username: ");

user1 = wordScan.next();
int attempts = 3;
boolean loggedIn = false;

attempts = attempts -1;
if(usernames.contains(user1)){

System.out.print("Now enter your password: ");

pass1=wordScan.next();

if(passwords.contains(pass1)){

System.out.println("Logged in.");
System.out.println("Your username is "+user1);
loggedIn = true;
random.setSeed(System.currentTimeMillis());
numGen = random.nextInt(899999)+100000;

System.out.println("Your access code is: "+numGen);
}
}
}

if(!loggedIn) {
System.out.println("Invalid Username or password... Terminating.");
System.exit(0);
}

wordScan.close();
}

关于java - 如何发起重试 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31742276/

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