题解 | #密码强度等级#

密码强度等级

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String pw = in.nextLine();
            int score = 0;
            boolean hasLetter = false;
            boolean hasLUCaseLetter = false;
            boolean hasNumber = false;
            boolean hasSymbol = false;
            if (pw.length() <= 4) {
                score += 5;
            } else if (pw.length() >= 5 && pw.length() <= 7) {
                score += 10;
            } else score += 25;

            String testLetter = pw.replaceAll("[^A-Za-z]+", "");
            if (testLetter.length() == 0) {
                hasLetter = false;
            } else if (testLetter.replaceAll("[A-Z]+", "").length() == 0 || testLetter.replaceAll("[A-Z]+" ,"").length() == testLetter.length()) {
                score += 10;
                hasLetter = true;
            } else {
                score += 20;
                hasLUCaseLetter = true;
            }

            String testNumber = pw.replaceAll("[^0-9]+", "");
            if (testNumber.length() == 1) {
                score += 10;
                hasNumber = true;
            } else if (testNumber.length() > 1) {
                score += 20;
                hasNumber = true;
            }

            String testSymbol = pw.replaceAll("[a-zA-Z0-9]+", "");
            if (testSymbol.length() == 1) {
                score += 10;
                hasSymbol = true;
            } else if (testSymbol.length() > 1) {
                score += 25;
                hasSymbol = true;
            }

            if (hasLUCaseLetter && hasNumber && hasSymbol) {
                score += 5;
            } else if (hasLetter && hasNumber && hasSymbol) {
                score += 3;
            } else if (hasLetter && hasNumber) {
                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");
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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