我正在尝试让我的密码验证程序循环,直到给出正确的响应。它现在正在循环,但它在第一次迭代后没有重置布尔值,无论哪个密码,都会在第二次产生有效响应。如何对每个值进行循环检查,或每次都重置表?
boolean atLeast8 = false;
boolean oneLower = false;
boolean oneUpper = false;
boolean oneNumber = false;
boolean oneSpecial = false;
boolean noAnd = false;
boolean noEnd = false;
do
{
System.out.println("Enter your password. " + '\n');
password = input.nextLine();
System.out.println();
for(int i=0; i<password.length(); i++) //Checks for values througout the length of the string
{
char c = password.charAt(i);
if(password.length() >= 8)
atLeast8 = true;
if(Character.isLowerCase(c))
oneLower = true;
if(Character.isUpperCase(c))
oneUpper = true;
if(Character.isDigit(c))
oneNumber = true;
if(c=='!' || c=='@' || c=='#' || c=='$' || c=='%' || c=='^' || c=='&' || c=='&' || c=='*')
oneSpecial = true;
if (password.indexOf("and") < 0)
noAnd = true;
if (password.indexOf("end") < 0)
noEnd = true;
}
if(atLeast8 && oneLower && oneUpper && oneNumber && oneSpecial && noAnd && noEnd) //Does the string contain any true values?
{
System.out.println("Valid.");
}
else
{
System.out.println("Invalid!"); //Does the string contain any false values?
}
if(!atLeast8)
{
System.out.println("Must be at least 8 characters long.");
}
if(!oneLower)
{
System.out.println("Must contain one lower case letter.");
}
if(!oneUpper)
{
System.out.println("Must contain one upper case letter.");
}
if(!oneNumber)
{
System.out.println("Must contain one numeric digit.");
}
if(!oneSpecial)
{
System.out.println("Must contain one special character.");
}
if(!noAnd)
{
System.out.println("Must not contain the word 'and'.");
}
if(!noEnd)
{
System.out.println("Must not contain the word 'end'.");
}
}while (atLeast8 == false || oneLower == false || oneUpper == false || oneNumber == false || oneSpecial == false || noAnd == false || noEnd == false);
}
}
答案 0 :(得分:0)
你不能只是把
atLeast8 = false;
oneLower = false;
oneUpper = false;
oneNumber = false;
oneSpecial = false;
noAnd = false;
noEnd = false;
在循环开始时(介于“do {”和“for”之间)
或者,这太简单了吗?