题解 | #密码强度等级#

密码强度等级

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

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String pwdV = sc.nextLine();

        //1、密码长度:
        int pwdLenPoint = 10;
        if (pwdV.length() >= 8) {
            pwdLenPoint = 25;
        } else if (pwdV.length() <= 4) {
            pwdLenPoint = 5;
        }

        //2、密码是否包含字母
        int pwdAaPoint = 0;
        if (containsZ("a-z", pwdV) || containsZ("A-Z", pwdV)) {
            pwdAaPoint = 10;
            if (containsZ("a-z", pwdV) && containsZ("A-Z", pwdV)) {
                pwdAaPoint = 20;
            }
        }
        //3、数字个数
        int pwdDigitPoint =containDigitCnt(pwdV) > 1?20:(containDigitCnt(pwdV) == 1?10:0)
;       
        //4、符号个数
        int signCnt=containSignCnt(pwdV);
        int pwdSignPoint = signCnt>1?25:(signCnt==1?10:0);


        //5、奖励
        int pwdAwardPoint = 0;
        if (pwdSignPoint > 0 & pwdDigitPoint > 0 ) {
            pwdAwardPoint = pwdAaPoint == 10 ? 3 : (pwdAaPoint == 20 ? 5 : 0);
        } else {
            pwdAwardPoint = pwdDigitPoint > 0 & pwdAaPoint > 0 ? 2 : 0;
        }

        int totalPoint = pwdAwardPoint + pwdDigitPoint + pwdSignPoint + pwdAaPoint +
                         pwdLenPoint;

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

    }

/*求字符个数,排除掉字符串里字母和数字个数,剩余为符号个数*/
 static int containSignCnt(String str) {
     
        char[] charArr = str.toCharArray();
        int count = 0;
        for (Character c : str.toCharArray()) {
            count = Character.isLetterOrDigit(c) ? ++count : count;
        }
        int signCnt=str.length()-count;
        return signCnt>0?signCnt:0;

    }


//求数字个数
    static int containDigitCnt(String str) {
        char[] charArr = str.toCharArray();
        int count = 0;
        for (Character c : str.toCharArray()) {
            count = Character.isDigit(c) ? ++count : count;
        }
        return count;

    }
    //是否含有字母
    static boolean containsZ(String pattern, String str) {
        Pattern p = Pattern.compile("[" + pattern + "]");
        Matcher matcher = p.matcher(str);
        return matcher.find();
    }
}

全部评论

相关推荐

07-03 11:02
中山大学 C++
字节刚oc,但距离九月秋招很近了有两段互联网实习,非腾讯字节。不敢赌转正,现在在纠结去还是不去如果实习俩月离职会有什么后果吗
阿城我会做到的:不去后悔一辈子,能否转正取决于ld的态度,只要他不卡,答辩就是走流程,个人觉得可以冲一把
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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