您的位置:首页 > 其它

密码验证合格程序

2017-09-02 16:27 483 查看
题目描述:

密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有相同长度超2的子串重复

 说明:长度超过2的子串

输入描述:一组或多组长度超过2的子符串。每组占一行

输出描述:如果符合要求输出:OK,否则输出NG

import
java.util.*;

public class Main {

    // 1.长度超过8位

    public static boolean checkLength(String password){

        if (password==null || password.length()<=8)

            return false;

        return true;

    }

    // 2.包括大小写字母.数字.其它符号,以上四种至少三种

    public static boolean checkCharKinds(String password){

        int Digit=0 , lowercase=0,uppercase=0,others=0;

        char[] ch = password.toCharArray();

        for (int i = 0; i < ch.length; i++) {

            if (ch[i]>='0'&&ch[i]<='9') {

                Digit=1;

                continue;

            }

            else if (ch[i]>='a'&&ch[i]<='z') {

                lowercase=1;

                continue;

            }

            else if (ch[i]>='A'&&ch[i]<='Z') {

                uppercase=1;

                continue;

            }else {

                others=1;

                continue;

            }

        }

        int total = Digit+lowercase+uppercase+others;

        return total>=3 ? true : false;

    }

    // 3.不能有相同长度超2的子串重复

    public static boolean checkCharRepeat(String password){

        for(int i=0 ;i<password.length()-2 ;i++){

            String substr1 =password.substring(i, i+3);

            if (password.substring(i+1).contains(substr1))

               return false;

        }

        return true;

    }

    public static void main(String[] args) {

        Scanner cin = new Scanner(System.in);

        while (cin.hasNextLine()) {

            String psw = cin.nextLine();

            if (checkLength(psw)&&checkCharKinds(psw)&&checkCharRepeat(psw))

                System.out.println("OK");

            else

                System.out.println("NG");

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: