题解 | #密码强度等级#

密码强度等级

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.hasNextLine()){
            String s = in.nextLine();
            int score = 0;
            int len = s.length();
            if(len <= 4) score+=5;
            else if(len>=5 && len<=7) score+=10;
            else score+=25;

            boolean hasUpper = false;
            boolean hasLower = false;
            boolean hasDigit = false;
            int countDigit = 0;
            boolean hasOther = false;
            int countOthers = 0;
            for(int i=0 ; i<len ; ++i){
                char c = s.charAt(i);
                if(Character.isLetter(c)){
                    if(c>='a' && c<='z'){
                        hasLower = true;
                    }
                    if(c>='A' && c<='Z'){
                        hasUpper = true;
                    }
                }
                else if(Character.isDigit(c)){
                    hasDigit = true;
                    countDigit++;
                }
                else{
                    hasOther = true;
                    countOthers++;
                }
            }
            if(hasLower || hasUpper){
                if(hasLower&&hasUpper){
                    score+=20;
                    //System.out.println(0);
                }
                else{
                    score+=10;
                }
            }
            if(hasDigit){
                if(countDigit==1){
                    score+=10;
                }
                else{
                    score+=20;
                }
            }
            if(hasOther){
                if(countOthers==1){
                    score+=10;
                }
                else{
                    score+=25;
                }
            }
            int sc1 = score, sc2=score, sc3=score;
            if(hasUpper||hasLower&&hasDigit){
                sc1+=2;
            }
            
            if(hasUpper||hasLower&&hasDigit&&hasOther){
                sc2+=3;
                
            }
            if(hasUpper&&hasLower&&hasDigit&&hasOther){
                sc3+=5;
            }
            score = Math.max(sc1, Math.max(sc2,sc3));
            //System.out.println(score);
            if(score>=90){
                System.out.println("VERY_SECURE");
            }
            else if(score<90 && score>=80){
                System.out.println("SECURE");
            }
            else if(score>=70 && score<80){
                System.out.println("VERY_STRONG");
            }
            else if(score>=60 && score<70){
                System.out.println("STRONG");
            }
            else if(score>=50 && score<60){
                System.out.println("AVERAGE");
            }
            else if(score>=25 && score<50){
                System.out.println("WEAK");
            }
            else{
                System.out.println("VERY_WEAK");
            }
            //System.out.println(score);
        }
    }
}

全部评论

相关推荐

09-22 09:42
门头沟学院 Java
牛客37185681...:马德,我感觉这是我面过最恶心的公司,一面是两个女hr,说什么实习前几个月属于试用期,试用期过了才能转成正式实习生,我***笑了,问待遇就是不说,问能不能接受全栈,沙币公司
如果可以选,你最想去哪家...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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