gpt4 book ai didi

java - 计数++ : While() wont stop loop after count reached

转载 作者:行者123 更新时间:2023-12-01 10:20:10 28 4
gpt4 key购买 nike

我一直在这里用头撞墙...可能与深夜有关,但是一旦我第三次输入变量“count”,我的 while 循环就不会结束。代码如下:

Scanner input = new Scanner(System.in);

final String CORRECT_PASSWORD = "CS1160";
String password;
int count = 0;

//ask for password

System.out.println("Please enter your password: ");
password = input.nextLine();

if(password.equals(CORRECT_PASSWORD)){
System.out.println("You have successfully logged in.");
}

count = 0;
while(count < 3){
while(!password.equals(CORRECT_PASSWORD))
{
System.out.println("Incorrect Password Entered.");
System.out.println("Please enter your password: ");
password=input.nextLine();
count++;

if(password.equals(CORRECT_PASSWORD)){
System.out.println("You have successfully logged in.");

count++;
}}



}
System.out.println("You have been locked out.");
}}

___________________END_______________________

  • 一切都编译得很好...根据我对 while 循环的理解,看起来很可靠,只是出于某种原因,一旦“Count”达到三,它就不会停止循环。也许有什么我忽略的事情吗?

非常感谢-萨姆

最佳答案

不要嵌套循环,使用 boolean 值和条件以及一个循环。比如,

while(count < 3 && !password.equals(CORRECT_PASSWORD))

关于java - 计数++ : While() wont stop loop after count reached,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35644582/

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