重置布尔值?(Reset Boolean values?)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
重置布尔值?(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?

更多推荐

本文发布于:2023-04-21 19:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/e3c5c1573cb1717db9dea78deff232e6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布尔值   Reset   values   Boolean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!