牛客密码验证合格

题目描述

密码要求
1、长度超过8位
2.包括大小写字母、数字、其他符号,以上4种至少三种
3.不能有相同长度超过2的子串重复 说明:长度超过2的子串

输入描述

一组或多组长度超过2的字符串。每组占一行

输出描述

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

public class AdminPassword {
    //String类中有一个String.matches() ,这个方法主要是
    // 返回是否匹配指定的字符串,如果匹配则为true,否则为false;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()) {
            String input = sc.nextLine();
            int counter = 0;//作用是验证要求2,每当密码中有一个类型时就+1
            if (input.matches(".*\\d.*")){
                counter++;
            }
            if (input.matches(".*[a-z].*")){
                counter++;
            }
            if (input.matches(".*[A-Z].*")){
                counter++;
            }
            if (input.matches(".*[^a-zA-Z0-9].*")) {
                counter++;
            }
            if (input.length() > 8 && counter >=3 && !input.matches(".*(...).*\\1.*")){
                System.out.println("OK");
            } else {
                System.out.println("NG");
            }
        }
    }
}

优秀:看看大佬写的


版权声明:本文为ludulge原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。