根据密码规则进行密码等级划分-Java

package com.patience.interview.huawei;

import scala.Char;

import java.util.Scanner;

/**
 * @author Green.Gee
 * @date 2022/12/19 14:33
 * @email green.gee.lu@gmail.com
 */
public class PasswordLevel {

    /**
     * 密码按如下规则进行计分,并根据不同的得分为密码进行安全等级划分。
     *
     * 一、密码长度:
     * 5 分: 小于等于4 个字符
     * 10 分: 5 到7 字符
     * 25 分: 大于等于8 个字符
     *
     * 二、字母:
     * 0 分: 没有字母
     * 10 分: 密码里的字母全都是小(大)写字母
     * 20 分: 密码里的字母符合”大小写混合“
     *
     * 三、数字:
     * 0 分: 没有数字
     * 10 分: 1 个数字
     * 20 分: 大于1 个数字
     *
     * 四、符号:
     * 0 分: 没有符号
     * 10 分: 1 个符号
     * 25 分: 大于1 个符号
     *
     * 五、奖励(只能选符合最多的那一种奖励):
     * 2 分: 字母和数字
     * 3 分: 字母、数字和符号
     * 5 分: 大小写字母、数字和符号
     *
     * 最后的评分标准:
     * >= 90: 非常安全
     * >= 80: 安全(Secure)
     * >= 70: 非常强
     * >= 60: 强(Strong)
     * >= 50: 一般(Average)
     * >= 25: 弱(Weak)
     * >= 0:  非常弱(Very_Weak)
     *
     * 对应输出为:
     *
     * VERY_SECURE
     * SECURE
     * VERY_STRONG
     * STRONG
     * AVERAGE
     * WEAK
     * VERY_WEAK
     *
     * 请根据输入的密码字符串,进行安全评定。
     *
     * 注:
     * 字母:a-z, A-Z
     * 数字:0-9
     * 符号包含如下: (ASCII码表可以在UltraEdit的菜单view->ASCII Table查看)
     * !"#$%&'()*+,-./     (ASCII码:0x21~0x2F)
     * :;<=>?@             (ASCII码:0x3A~0x40)
     * [\]^_`              (ASCII码:0x5B~0x60)
     * {|}~                (ASCII码:0x7B~0x7E)
     *
     * 提示:
     * 1 <= 字符串的长度<= 300
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String n = in.nextLine();
            int score = 0;
            int len = n.length();
            if(len >0 &&len <= 4){
                score += 5;
            }else if(len >= 5 && len <= 7){
                score += 10;
            }else if(len >= 8){
                score += 25;
            }
            int letterScore = 0;
            if(check(n,len,1)){
                if(check(n,len,2)){
                    score += 10;
                    letterScore = 1;
                    if(check(n,len,3)){
                        letterScore = 2;
                        score += 10;
                    }
                }else if(check(n,len,3)){
                    score += 10;
                    letterScore = 1;
                    if(check(n,len,2)){
                        score += 10;
                        letterScore = 2;
                    }
                }
            }
            int digitScore = 0;
            if(check(n,len,4)){
                digitScore = 1;
                if(digitCount(n) == 1){
                    score += 10;
                }else{
                    score += 20;

                }
            }
            int chFlag = 0;
            int chCount = chCount(n);
            if(chCount > 1){
                chFlag = 1;
                score += 25;
            }else if(chCount == 1){
                score += 10;
                chFlag = 1;
            }
            if(chFlag > 0 && digitScore > 0 && letterScore == 2){
                score += 5;
            }else if(letterScore == 1 && chFlag > 0 && digitScore > 0){
                score += 3;
            }else if(letterScore ==1 && digitScore > 0){
                score += 2;
            }

            System.err.println(score);
//返回等级
            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 if(score>=0)
                System.out.println("VERY_WEAK");


        }
    }

    static int chCount(String n){
        int i = 0;
        int count = 0;
        while (i < n.length()) {
            char ch = n.charAt(i);
            if (ch >= 32 && ch <= 47 ||
                    ch >= 58 && ch <= 64 ||
                    ch >= 91 && ch <= 96 ||
                    ch >= 123 && ch <= 126) {
                count++;
            }
            i++;
        }
        return count;
    }

    static int digitCount(String n) {
        int i = 0;
        int count = 0;
        while (i < n.length()) {
            char ch = n.charAt(i);
            if (Character.isDigit(ch)) {
                count++;
            }
            i++;
        }
        return count;
    }
    static boolean check(String n,int len,int type){
        int i = 0;
        boolean flag = false;
        if(1 == type){
            while(i < len){
                char ch = n.charAt(i);
                if(Character.isLetter(ch)){
                    flag = true;
                    break;
                }
                i++;
            }
        }else if(2 == type){
            while(i < len){
                char ch = n.charAt(i);
                if(Character.isLowerCase(ch)){
                    flag = true;
                    break;
                }
                i++;
            }
        }else if(3 == type){
            while(i < len){
                char ch = n.charAt(i);
                if(Character.isUpperCase(ch)){
                    flag = true;
                    break;
                }
                i++;
            }
        }else if(4 == type){
            while(i < len){
                char ch = n.charAt(i);
                if(Character.isDigit(ch)){
                    flag = true;
                    break;
                }
                i++;
            }
        }
        return flag;
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P("Struggler") ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值