题解 | 密码强度等级

解题思路

  1. 很简单,按要求不断判断即可,就是显得比较繁琐
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String pwd = in.next();
        int score = 0;
        score += lengthScore(pwd.length());
        char[] pArr = pwd.toCharArray();
        // 1 表示小写字母,2表示大写字母,3表示数字,4表示符号
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1,0);
        map.put(2,0);
        map.put(3,0);
        map.put(4,0);
        for (int i = 0; i < pArr.length; i++) {
            char c = pArr[i];
            if (c >= 'a' && c <= 'z') {
                map.put(1, map.getOrDefault(1, 0) + 1);
            } else if (c >= 'A' && c <= 'Z') {
                map.put(2, map.getOrDefault(2, 0) + 1);
            } else if (c >= '0' && c <= '9') {
                map.put(3, map.getOrDefault(3, 0) + 1);
            } else {
                map.put(4, map.getOrDefault(4, 0) + 1);
            }
        }

        // 字母
        if (map.get(1) > 0 && map.get(2) > 0) {
            score += 20;
        } else if (map.get(1) > 0 || map.get(2) > 0) {
            score += 10;
        }

        // 数字
        if (map.get(3) > 1) {
            score += 20;
        } else if (map.get(3) > 0) {
            score += 10;
        }

        // 符号
        if (map.get(4) > 1) {
            score += 25;
        } else if (map.get(4) > 0) {
            score += 10;
        }

        // 奖励
        if (map.get(1) > 0 && map.get(2) > 0 && map.get(3) > 0 && map.get(4) > 0) {
            score += 5;
        } else if ((map.get(1) > 0 || map.get(2) > 0) && map.get(3) > 0 &&
                   map.get(4) > 0) {
            score += 3;
        } else if ((map.get(1) > 0 || map.get(2) > 0) && map.get(3) > 0) {
            score += 2;
        }

        if (score >= 90) {
            System.out.println("VERY_SECURE");
        } else if (score >= 80) {
            System.out.println("SECURE");
        } else if (score >= 70) {
            System.out.println("VERY_STRONG");
        } else if (score >= 60) {
            System.out.println("STRONG");
        } else if (score >= 50) {
            System.out.println("AVERAGE");
        } else if (score >= 25) {
            System.out.println("WEAK");
        } else {
            System.out.println("VERY_WEAK");
        }

    }

    public static int lengthScore(int len) {
        if (len <= 4) {
            return 5;
        } else if (len >= 5 && len <= 7) {
            return 10;
        } else {
            return 25;
        }
    }
}

全部评论

相关推荐

Java面试先知:我也是和你一样的情况,hr 说等开奖就行了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务