Java解决判断新密码是否符合规则

img

import java.util.Scanner;

public class Q2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

    long n = input.nextInt();
    String z = input.nextLine();
    for(int i = 0;i < n;i++){
        String str = input.nextLine();
        boolean a = t1(str);
        boolean b = t2(str);
        boolean c = t3(str);
        if(a && b && c)
            System.out.println("True");
        else
            System.out.println("False");
    }
}
public static boolean t1(String s){
    if(s.length() <= 15 && s.length() >= 8)
        return true;
    else
        return false;
}
public static boolean t2(String s){
    int b = 0;
    int c = 0;
    int d = 0;
    for(int i = 0;i < s.length();i++){
        if(s.charAt(i) >= 'A'&& s.charAt(i) <= 'Z')
            b++;
        if(s.charAt(i) >= 'a'&& s.charAt(i) <= 'z')
            c++;
        if((s.charAt(i) >= '0'&& s.charAt(i) <= '9')|| s.charAt(i) == 35||s.charAt(i) == 36||s.charAt(i) == 94||s.charAt(i) == 38||s.charAt(i) == 42)
            d++;
    }
    if(b > 0 && c > 0 && d > 0)
        return true;
    else
        return false;
}
public static boolean t3(String s){
    String[] a = s.split("");
    for(int i = 0;i < s.length() - 2;i++){
        if(a[i].equals(a[i+1]) && a[i].equals(a[i+2]) &&((s.charAt(i) >= 'A'&& s.charAt(i) <= 'Z')||(s.charAt(i) >= 'a'&& s.charAt(i) <= 'z')||(s.charAt(i) >= '0'&& s.charAt(i) <= '9')))
            return false;
    }
    return true;
}

}

img

用三个method,第一个判断长度,第二个判断是否含有大小写以及数字或者特殊字符 ,第三个判断是否含有三个连续的数字或字母(允许三个连续的特殊符号)

全部通过

最为简便的代码就是直接上正则表达式