Java 如何修复我的代码,以便找到我设置的密码标准
每次我试图让用户生成密码时,无论我是否按照我设置的标准键入密码,它都会返回为false 我多次尝试重写检查方法,但没有成功。我试着用ArrayList来比较Java 如何修复我的代码,以便找到我设置的密码标准,java,Java,每次我试图让用户生成密码时,无论我是否按照我设置的标准键入密码,它都会返回为false 我多次尝试重写检查方法,但没有成功。我试着用ArrayList来比较 public boolean check(String password) { if(password.length() < 6 && password.length() > 16) { return false; } Pattern special = Pa
public boolean check(String password)
{
if(password.length() < 6 && password.length() > 16)
{
return false;
}
Pattern special = Pattern.compile (specialChars);
Matcher hasSpecial = special.matcher(password);
int i = 0;
boolean hasDigit = false;
boolean hasLower = false;
boolean hasUpper = false;
while(i < password.length() && !hasDigit && !hasLower && !hasUpper)
{
if(Character.isDigit(password.charAt(i)))
{
hasDigit = true;
}
else if(Character.isLowerCase(password.charAt(i)))
{
hasLower = true;
}
else if(Character.isUpperCase(password.charAt(i)))
{
hasUpper = true;
}
i++;
}
return hasDigit && hasUpper && hasLower && hasSpecial.find();
}
公共布尔检查(字符串密码)
{
if(password.length()<6&&password.length()>16)
{
返回false;
}
Pattern special=Pattern.compile(specialChars);
Matcher hasSpecial=special.Matcher(密码);
int i=0;
布尔hasDigit=false;
布尔hasLower=false;
布尔hassupper=false;
while(i
如果密码小于6个字符且大于16个字符,我希望密码返回false。此外,密码必须包含一个数字、一个大写字母、一个小写字母和一个特殊字符。您的while语句错误:
while(i < password.length() && !hasDigit && !hasLower && !hasUpper)
while(i
当其中一个标志变为true时,while循环将退出,因为它将变为false(符号),表达式的计算结果将变为false
为了进一步解释,假设密码的第一个字符是数字。然后,标志hasDigit将变为true。所有其他标志都是假的
表达方式:
while(i < password.length() && !hasDigit && !hasLower && !hasUpper)
while(i
转向:
while(i < password.length() && false && true && true)
while(i
整个表达式的计算结果为false
考虑修改while语句以符合所需的条件。您的代码有两个问题 1) while条件。 要满足您的标准,它应该是
while(i < password.length() && !(hasDigit && hasLower && hasUpper))
首先,第一个如果从来都不是真的。长度不能小于6,也不能大于16。将其更改为或
if(password.length() < 6 || password.length() > 16)
if(password.length()<6 | | password.length()>16)
关于while循环,删除除
i < password.length()
i
您总是希望迭代密码中的所有字符 使用while循环将永远无法完成所有字符的循环,也不需要使用while循环
public boolean check(String password) {
if (password.length() < 6 && password.length() > 16) {
return false;
}
boolean hasSpecial = password.matches(specialChars);
boolean hasDigit = password.matches(".*\\d+.*");
boolean hasLower = !password.equals(password.toUpperCase());
boolean hasUpper = !password.equals(password.toLowerCase());
return hasDigit && hasUpper && hasLower && hasSpecial;
}
公共布尔检查(字符串密码){
if(password.length()<6&&password.length()>16){
返回false;
}
布尔haspecial=password.matches(specialChars);
boolean hasDigit=password.matches(“.\\d+.”);
布尔值hasLower=!password.equals(password.toUpperCase());
布尔值hasUpper=!password.equals(password.toLowerCase());
返回hasDigit&&hasUpper&&hasLower&&hasSpecial;
}
公共布尔检查(字符串密码){
布尔hasDigital、hasLower、hasUpper、hasSpecial;
hasDigit=hasLower=hasUpper=hasSpecial=false;
如果(password.length()>=6&&password.length(),我也会在password.length()<6&&password.length()>16
中进行编辑。另一方面,请注意:永远不要将密码存储在字符串对象中!字符串对象在内存中的时间可能比您想要的长,并且由于字符串是不可变的,您无法覆盖它们。请使用char[]相反,您可以使用Arrays.fill(…)在使用后删除密码数据。
public boolean check(String password) {
if (password.length() < 6 && password.length() > 16) {
return false;
}
boolean hasSpecial = password.matches(specialChars);
boolean hasDigit = password.matches(".*\\d+.*");
boolean hasLower = !password.equals(password.toUpperCase());
boolean hasUpper = !password.equals(password.toLowerCase());
return hasDigit && hasUpper && hasLower && hasSpecial;
}
public boolean check(String password) {
boolean hasDigit, hasLower, hasUpper, hasSpecial;
hasDigit = hasLower = hasUpper = hasSpecial = false;
if (password.length() >= 6 && password.length() <= 16) {
int i = 0;
while (i < password.length()) {
if(Character.isDigit(password.charAt(i))) {
hasDigit=true;
}
else if(Character.isLowerCase(password.charAt(i))) {
hasLower=true;
}
else if(Character.isUpperCase(password.charAt(i))) {
hasUpper=true;
}
else if(!Character.isLetterOrDigit(password.charAt(i))) {
hasSpecial=true;
}
if (hasDigit && hasLower && hasUpper && hasSpecial) {
return true;
}
i++;
}
}
return false;
}