猿问

如何修复我的代码,以便它找到我设置的密码

每次我尝试让用户生成密码时,它总是会返回为假的,无论我是否按照我设置的标准输入它。


我曾多次尝试重写检查方法,但没有运气。我尝试使用数组列表来比较。


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();

}

我希望密码返回 false,如果它小于 6 个字符且超过 16 个字符。此外,密码必须包含1个数字,一个大写字母,一个小写字母和一个特殊字符


犯罪嫌疑人X
浏览 115回答 5
5回答

神不在的星期二

您的 while 语句是错误的:while(i&nbsp;<&nbsp;password.length()&nbsp;&&&nbsp;!hasDigit&nbsp;&&&nbsp;!hasLower&nbsp;&&&nbsp;!hasUpper)当其中一个标志变为 true 时,while 循环将退出,因为它将变为 false(! 符号),表达式的计算结果将为 false。为了进一步解释,假设密码的第一个字符是一个数字。然后,具有数字的旗帜将变为真。所有其他标志都是假的。表达式:while(i&nbsp;<&nbsp;password.length()&nbsp;&&&nbsp;!hasDigit&nbsp;&&&nbsp;!hasLower&nbsp;&&&nbsp;!hasUpper)转至 :while(i&nbsp;<&nbsp;password.length()&nbsp;&&&nbsp;false&nbsp;&&&nbsp;true&nbsp;&&&nbsp;true)整个表达式的计算结果为 false。考虑修改您的 while 语句以符合您要查找的条件。

慕斯709654

public boolean check(String password) {&nbsp; &nbsp; &nbsp; &nbsp; boolean hasDigit, hasLower, hasUpper, hasSpecial;&nbsp; &nbsp; &nbsp; &nbsp; hasDigit = hasLower = hasUpper = hasSpecial = false;&nbsp; &nbsp; &nbsp; &nbsp; if (password.length() >= 6 && password.length() <= 16) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (i < password.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(Character.isDigit(password.charAt(i))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasDigit=true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(Character.isLowerCase(password.charAt(i))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasLower=true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(Character.isUpperCase(password.charAt(i))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasUpper=true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(!Character.isLetterOrDigit(password.charAt(i))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasSpecial=true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (hasDigit && hasLower && hasUpper && hasSpecial) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }

至尊宝的传说

您的代码有两个问题1)同时条件。要满足您的标准,它应该是&nbsp;while(i < password.length() && !(hasDigit && hasLower && hasUpper))&nbsp;这将确保即使单个值为 false,循环也会一直持续到字符串长度的末尾boolean(hasDigit/hasLower/hasUpper)password2) 在模式初始化中定义的特殊字符字符串。它应该是这样的:[#$&!]以下是完整的代码片段public class Snippet {public static void main(String[] args ) {&nbsp; &nbsp; Snippet snippet = new Snippet();&nbsp; &nbsp; System.out.println(snippet.check("Abcd#fg1d#"));}public boolean check(String password)&nbsp;{&nbsp; &nbsp; if(password.length() < 6 && password.length() > 16)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; Pattern special = Pattern.compile ("[#!]");&nbsp; &nbsp; Matcher hasSpecial = special.matcher(password);&nbsp; &nbsp; int i = 0;&nbsp; &nbsp; boolean hasDigit = false;&nbsp; &nbsp; boolean hasLower = false;&nbsp; &nbsp; boolean hasUpper = false;&nbsp; &nbsp; while(i < password.length() && (!hasDigit || !hasLower || !hasUpper))&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(Character.isDigit(password.charAt(i)))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasDigit = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else if(Character.isLowerCase(password.charAt(i)))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasLower = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else if(Character.isUpperCase(password.charAt(i)))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasUpper = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; }&nbsp; &nbsp; return hasDigit && hasUpper && hasLower && hasSpecial.find();}}

哈士奇WWW

你永远不会用它完成所有字符的循环,同时循环,你也不需要太使用一个while循环&nbsp; &nbsp; public boolean check(String password) {&nbsp; &nbsp; &nbsp; &nbsp; if (password.length() < 6 && password.length() > 16) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; boolean hasSpecial = password.matches(specialChars);&nbsp; &nbsp; &nbsp; &nbsp; boolean hasDigit = password.matches(".*\\d+.*");&nbsp; &nbsp; &nbsp; &nbsp; boolean hasLower = !password.equals(password.toUpperCase());&nbsp; &nbsp; &nbsp; &nbsp; boolean hasUpper = !password.equals(password.toLowerCase());&nbsp; &nbsp; &nbsp; &nbsp; return hasDigit && hasUpper && hasLower && hasSpecial;&nbsp; &nbsp; }

繁星coding

首先,第一个如果永远不会是真的。长度不能短于 6 且长于 16。将其更改为或if(password.length()&nbsp;<&nbsp;6&nbsp;||&nbsp;password.length()&nbsp;>&nbsp;16)关于 while 循环,删除除i&nbsp;<&nbsp;password.length()您始终希望迭代密码中的所有字符。
随时随地看视频慕课网APP

相关分类

Java
我要回答