布尔返回;我不知道如何返回我想要的结果

时间:2014-08-05 06:01:16

标签: java boolean

我正在进行PW验证。我在这里找到了另一个用户开发的代码,并且正在使用它(用户:Quirliom)。我的问题是如果PW满足所有要求,它吐出1:PW验证。如果没有,它吐出-1:PW未经验证。在使用该代码的布尔方法中,如果我状态返回true,则用户输入的PW是否满足要求,则说明PW满足要求。我如何改变它,如果它符合要求,真的会来,如果没有,它将是假的?

import java.util.*;
import java.lang.String;
import java.lang.Character;

public class Password {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Enter password: ");
    String password = input.next();
    if (isValid(password)) {
        System.out.println("1: Valid Password");
    } else {
        System.out.println("-1: Invalid Password");
    }
}


//Invoke method for PW verification
public static boolean isValid(String password) {

    //Checks if PW is at least 8 characters
    boolean isAtLeast8 = password.length() >= 8;//Checks for at least 8 characters
    if(!isAtLeast8)System.out.println("Must be 8 or more characters.");

    //PW cannot exceed 24 characters
    boolean isAtLeast24 = password.length() <= 24;
    if(!isAtLeast24)System.out.println("Must not exceed 24 characters.");

    //Checks if PW is only letters and digits - no special characters
    //Checks at least one char is not alpha numeric
    boolean hasSpecial = !password.matches(".*[!@#$%^&*].*");
    if(!hasSpecial)System.out.println("Must not contain special character !@#$%^&*.");

    //Check PW to have 2 uppercase
    boolean hasUppercase = !password.equals(password.toLowerCase());
    if(!hasUppercase)System.out.println("Must have 2 uppercase Character");

    //Check PW to have 2 lowercase
    boolean hasLowercase = !password.equals(password.toUpperCase());
    if(!hasLowercase)System.out.println("Must have 2 lowercase Character");

    //Check PW to have 2 digits
    boolean hasNumber = password.matches(".*\\d.*");
    if(!hasNumber)System.out.println("Must have 2 digits");

return true;

}

}

1 个答案:

答案 0 :(得分:4)

看起来你想要这个:

return isAtLeast8 && isAtLeast24 && hasSpecial && hasUppercase && hasLowercase && hasNumber;