题解 | #密码强度等级#

密码强度等级

https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String password = scanner.nextLine();

            int len = password.length();
            int numberCount = 0;
            int lowerLetterCount = 0;
            int upperLetterCount = 0;
            int symbolCount = 0;
            int difficulty = 0;

            for (int i = 0; i < password.length(); i++) {
                char c = password.charAt(i);
                if (c <= '9' && c >= '0') {
                    numberCount++;
                } else if (c <= 'z' && c >= 'a') {
                    lowerLetterCount++;
                } else if (c <= 'Z' && c >= 'A') {
                    upperLetterCount++;
                } else {
                    symbolCount++;
                }
            }

            difficulty += len <= 4 ? 5 : len < 8 ? 10 : 25;
            difficulty += lowerLetterCount > 0 && upperLetterCount > 0 ? 20
                    : lowerLetterCount == 0 && upperLetterCount == 0 ? 0 : 10;
            difficulty += numberCount > 1 ? 20 : numberCount * 10;
            difficulty += symbolCount > 1 ? 25 : symbolCount * 10;
            int reward = 0;
            if ((lowerLetterCount > 0 || upperLetterCount > 0) && numberCount > 0) {
                reward += 2;
                if (symbolCount > 0) {
                    reward += 1;
                    if (lowerLetterCount > 0 && upperLetterCount > 0) {
                        reward += 2;
                    }
                }
            }
            difficulty += reward;
            // System.out.println(difficulty);
            String[] levels = {"AVERAGE", "STRONG", "VERY_STRONG", "SECURE", "VERY_SECURE"};
            String difficultyLevel = difficulty < 25 ? "VERY_WEAK"
                    : difficulty < 50 ? "WEAK"
                    : levels[(difficulty / 10) - 5];
            System.out.println(difficultyLevel);
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-26 15:46
已编辑
字节国际 电商后端 24k-35k
点赞 评论 收藏
分享
专心打鱼:互联网搬运工,贴子都要偷
点赞 评论 收藏
分享
无敌虾孝子:喜欢爸爸还是喜欢妈妈
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务