我是一名正在学习 IB 计算机科学的初学者程序员,正在根据课本进行练习。用户输错密码3次后如何结束这个循环。
public class Password
{
public static void main(String[]args)
{
int remaining = 3;
String reenter="hello";
do
{
String password=IBIO.inputString("Please enter the password: ");
if (password.indexOf("hello")>-1)
{
System.out.println("Welcome");
}
else
{
System.out.println("Access Denied");
remaining--;
}
reenter=IBIO.inputString("Enter the password: ");
}
while (reenter.equals("hello"));
while (remaining > 0);
}
}
这就是现在的样子,但当我输入错误的密码时,它只说访问被拒绝一次,然后就无法显示任何内容。我很确定我不能像这样互相领先两段时间。而且,当我正确输入密码时,它会不断要求我输入密码,这样它就不会关闭循环。只有每次我正确输入时,它才会说“欢迎”
最佳答案
引入计数器
int remaining = 3;
并在else
分支中递减它:
remaining--
检查它是否大于0。更换该行
while (reenter.equals("hello"));
这样:
while (remaining > 0);
关于java - 创建密码应用程序。密码输入错误3次后如何结束循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19136976/