EX: 判断密码, 判断字符必须包含大写,小写,数字,特殊字符 ,并且键盘不能连续

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by Administrator on 2019/12/17
 */
public class PassWord_Check {
    public static final String PW_PATTERN = "^(?![A-Za-z0-9]+$)(?![a-z0-9\W]+$)(?![A-Za-z\W]+$)(?![A-Z0-9\W]+$)[a-zA-Z0-9\W]{8,}$";
    public static void main(String []args ) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入字符!");
        String password=input.nextLine();
        boolean a1=false;
        if (password.length() >7 ) {
            a1=true;
            System.out.println("1.满足长度不小于8~");
        }  //判断不为空,并且长度不小于8
        else{
            System.out.println("1.不满足长度不小于8~");
        }

        boolean a2=false;
        if (password.matches(PW_PATTERN)){
            a2=true;
            System.out.println("2.满足有小写,大写,数字,特殊字符!");
        }
        else{
            System.out.println("2.不满足必须包含大写,小写,数字,特殊字符!");
        }
        //判断有数字,字母,特殊字符

        boolean a3=new PassWord_Check().test5(password);
        if (a3==true){
            System.out.println("3.满足键盘不连续");
        }
        else{
            System.out.println("3.不满足键盘不连续.");
        }
        //判断字母键盘是否连续,符合不连续,返回真,否则,返回假

        String [] dict=new String[]{"password","admin","root",
        "012","123","234","345","456","567","678","789","987",
        "876","765","654","543","432","321","210"};
        boolean a4=true;
        for (String s:dict){
            if (password.contains(s)){
                a4=false;
                //判断不包含连续数字,以及保留字,用的穷举
            }
        }
        

        int ans=0;
        if (a1==true){
            if (a2==true){
                if (a3==true){
                    if (a4==true){
                        ans=1;
                        //全部满足条件,设为1,用作后用
                    }
                }


            }
        }
        if (ans==1){
            System.out.println("符合规范");
        }
        else{
            System.out.println("不符合规范");
        }


    }

    //判断键盘不连续
     boolean test5(String password) {
        char[][] keyCode = {
                {'q','w','e','r','t','y','u','i','o','p'},
                {'a','s','d','f','g','h','j','k','l'},
                {'z','x','c','v','b','n','m'}
        };

        char[] c = password.toCharArray();
        List<Integer> x = new ArrayList<Integer>();
        List<Integer> y = new ArrayList<Integer>();
        for (int i = 0; i < c.length; i++) {
            char temp = c[i];
            toHere:
            for(int j=0;j<keyCode.length;j++){
                for(int k=0;k<keyCode[j].length;k++){
                    if(temp== keyCode[j][k]){
                        x.add(j);
                        y.add(k);
                        break toHere;
                    }
                }
            }
        }
        boolean flag= true;
        for(int i=0;i<x.size()-2;i++){
// x                 b
            if(x.get(i)==x.get(i+1)&&x.get(i+1) == x.get(i+2)){//三者在同一y行上
                if(y.get(i)>y.get(i+2)){
                    if( y.get(i)-1 == y.get(i+1) && y.get(i)-2 == y.get(i+2)){
                        flag=false;
                        break;
                    }
                }else{
                    if( y.get(i)+1 == y.get(i+1) && y.get(i)+2 == y.get(i+2)){
                        flag=false;
                        break;
                    }
                }

            }else if(x.get(i)!=x.get(i+1)&&x.get(i+1) != x.get(i+2) &&x.get(i)!=x.get(i+2)){//三者均不在同一行上
                if(x.get(i)>x.get(i+2)){
                    if((x.get(i)-1 == x.get(i+1) && x.get(i)-2 == x.get(i+2))){
                        flag=false;
                    }
                }else{
                    if( x.get(i)+1 == x.get(i+1) && x.get(i)+2 == x.get(i+2)){
                        flag=false;
                        break;
                    }
                }
            }
        }
        return flag;
    }
}
原文地址:https://www.cnblogs.com/alpha-cat/p/12054938.html