题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

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
            System.out.println(check(in.nextLine()) ? "OK" : "NG");
        }
    }

    public static boolean check(String pswd){
        if(pswd.length() <= 8) return false;
        int counte = 0;
        int countE = 0;
        int countN = 0;
        int countS = 0;
        /*for(int i=0; i<pswd.length(); i++){
            char c = pswd.charAt(i);
            if(c>='0' && c<='9') countN++;
            else if(c>='a' && c<='z') counte++;
            else if(c>='A' && c<='Z') countE++;
            else if(c != ' ' || c == '\n') countS++;
        }
        if((counte>0 && countE>0 && countN>0) 
        || (counte>0 && countE>0 && countS>0)
        || (counte>0 && countN>0 && countS>0)
        || (countE>0 && countN>0 && countS>0)
        || (counte>0 && countE>0 && countN>0 && countS>0)){
            return reStr(pswd);
        }*/
        for(int i=0; i<pswd.length(); i++){
            char c = pswd.charAt(i);
            if(c>='0' && c<='9') countN=1;
            else if(c>='a' && c<='z') counte=1;
            else if(c>='A' && c<='Z') countE=1;
            else if(c != ' ' || c == '\n') countS=1;
        }
        if(counte+countE+countN+countS >= 3) return reStr(pswd);
        return false;
    }

    private static boolean reStr(String s) {
        for (int i=3; i<s.length(); i++) {
            if (s.substring(i).contains(s.substring(i-3, i))) {
                return false;
            }
        }
        return true;
    }
}

全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
04-02 20:12
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务