Java密码检查器布尔返回值无法正常工作

时间:2018-01-12 22:17:25

标签: java

当我通过测试用例运行此类时,代码每次都返回false。我假设这是因为我的标志在我的帮助方法中无法正常工作,因为我将它们全部设置为false。然而,当我尝试将它们取出,并用just return true或return false替换它们时,eclipse一直告诉我没有返回值。你们有没有看到什么出错?提前谢谢。

public static boolean isLevel1(String pwd) {
    int howMany = 0;
    if(isLongEnough(pwd) && hasNoSpaces(pwd)) {
        if(atLeastOneUpper(pwd)) howMany++;
        if(atLeastOneLower(pwd)) howMany++;
        if(atLeastOneDigit(pwd)) howMany++;
        if(howMany >= 2)
            return true;
    }
    return false;

}


public static boolean isLevel2(String pwd) {
    if(isLongEnough(pwd) &&
            hasNoSpaces(pwd) &&
            atLeastOneUpper(pwd) &&
            atLeastOneLower(pwd) &&
            atLeastOneDigit(pwd))
        return true;
    return false;
}   



private static boolean isLongEnough(String pwd) {
    if(pwd.length() >=6)
        return true;
    return false;
}

private static boolean hasNoSpaces(String pwd) {
    boolean flag = false;
    for(int i = 0; i<pwd.length(); i++) {
        if(!pwd.contains(" "))
            flag = true;
    }
    return flag;
}

private static boolean atLeastOneUpper(String pwd) {
    boolean flag = false;
    for(int i = 0; i<pwd.length(); i++) {
        if(Character.isUpperCase(i))
            flag = true;
    }
    return flag;
}

private static boolean atLeastOneLower(String pwd) {
    boolean flag = false;
    for(int i = 0; i<pwd.length(); i++) {
        if(Character.isLowerCase(i))
            flag = true;
    }
    return flag; 
}

private static boolean atLeastOneDigit(String pwd) {
    boolean flag = false;
    for(int i = 0; i<pwd.length(); i++) {
        if(Character.isDigit(i))
            flag = true;
}
    return flag;

}

}

1 个答案:

答案 0 :(得分:0)

为时已晚,但您使用Character.isXxxx(i)juste将其替换为Character.isXxx(pwd.charAt(i))

isLongEnough true
hasNoSpaces true
atLeastOneUpper true
atLeastOneLower true
atLeastOneDigit true
isLevel1 false
isLevel1 false
isLevel1 true
isLevel2 false
isLevel2 true
isLevel2 true

输出:

    private static boolean isLongEnough(String pwd) {
    return pwd.length() >= 6;
}

private static boolean hasNoSpaces(String pwd) {
    return !pwd.contains(" ");
}

2种方法可以简化

0 -> can precede either 1 or 0
  -> 01
  -> 00

1 -> can precede 0
  -> 10

01 -> 010
00 -> 000
      001
10 -> 100
      101