题解 | #密码强度等级#答案就在题目中,按部就班即可

密码强度等级

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int sumScore = 0;
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String passWord = in.nextLine();
            sumScore = 0;
            sumScore += getScoreByLength(passWord);
            sumScore += getScoreByLetter(passWord);
            sumScore += getScoreByNumber(passWord);
            sumScore += getScoreByOther(passWord);
            sumScore += getScoreByReward(passWord);
            if(sumScore>=90){
                System.out.println("VERY_SECURE");
            }else if(sumScore>=80){
                System.out.println("SECURE");
            }else if(sumScore>=70){
                System.out.println("VERY_STRONG");
            }else if(sumScore>=60){
                System.out.println("STRONG");
            }else if(sumScore>=50){
                System.out.println("AVERAGE");
            }else if(sumScore>=25){
                System.out.println("WEAK");
            }else{
                System.out.println("VERY_WEAK");
            }
        }
    }
    public static int getScoreByLength(String str){
            if(str.length() <= 4){
                return 5;
            }else if(str.length()>=5 && str.length()<=7){
                return 10;
            }
            return 25;
    }
    public static int getScoreByLetter(String str){
        boolean haveUpper = false,haveLower = false;
        for(Character c:str.toCharArray()){
            if(Character.isUpperCase(c)){
                haveUpper = true;
            }else if(Character.isLowerCase(c)){
                haveLower = true;
            }
        }
        if(haveUpper && haveLower){
            return 20;
        }else if(haveUpper || haveLower) {
            return 10;
        }
        return 0;
    }
    public static int getScoreByNumber(String str){
        int count = 0;
        for(Character c:str.toCharArray()){
            if(Character.isDigit(c)){
                count++;
            }
        }
        if(count>1){
            return 20;
        }else if(count==1){
            return 10;
        }
        return 0;
    }
    public static int getScoreByOther(String str){
        int countOther = 0;
        for(Character c:str.toCharArray()){
            if(!Character.isDigit(c) && !Character.isLetter(c)){//非字符和数字
                countOther++;
            }
        }
        if(countOther>1){
            return 25;
        }else if(countOther==1){
            return 10;
        }
        return 0;
    }
    public static int getScoreByReward(String str){
        if(getScoreByLetter(str)==20 && getScoreByNumber(str)!=0 && getScoreByOther(str)!=0){
            return 5;
        }else if(getScoreByLetter(str)==10 && getScoreByNumber(str)!=0 && getScoreByOther(str)!=0){
            return 3;
        }else if(getScoreByLetter(str)!=0 && getScoreByNumber(str)!=0 && getScoreByOther(str)==0){
            return 2;
        }
        return 0;
    }
}
全部评论

相关推荐

点赞 评论 收藏
转发
投递字节跳动等公司10个岗位
点赞 评论 收藏
转发
点赞 评论 收藏
转发
点赞 1 评论
分享
牛客网
牛客企业服务