重置布尔值?(Reset Boolean values?)
我试图让我的密码验证程序循环,直到给出正确的响应。 它现在正在循环,但它在第一次迭代后没有重置布尔值,无论哪个密码都是第二次产生有效响应。 如何对每个值进行循环检查,或每次重置表?
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); } }I am trying to get my password verifier to loop until a correct response is give. It's looping right now, but its not resetting the boolean values after the first iteration, resulting in a valid response the second time no matter which password. How do I make the loop check for every value, or reset the table every time?
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); } }最满意答案
你不能放
atLeast8 = false; oneLower = false; oneUpper = false; oneNumber = false; oneSpecial = false; noAnd = false; noEnd = false;在循环开始时(介于“do {”和“for”之间)
或者,这太简单了吗?
Can't you just put
atLeast8 = false; oneLower = false; oneUpper = false; oneNumber = false; oneSpecial = false; noAnd = false; noEnd = false;at the start of the loop (somewhere between the "do {" and the "for")
Or, is that being too simplistic?
更多推荐
发布评论