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

密码验证合格程序

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.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            if (validPWD(str)) {
                System.out.println("OK");
            } else {
                System.out.println("NG");
            }
        }
    }

    public static boolean validPWD(String str) {
        int count = 0;
        if(isDit(str))count++;
        if(isLower(str))count++;
        if(isUpper(str))count++;
        if(isOther(str))count++;

        return count >= 3 && !iscommonStr(str) && str.length() >= 8;
    }

    public static boolean iscommonStr(String str) {
        for(int i = 0; i < str.length() - 4; i++){
            String cur = str.substring(i, i + 3);
            String leftover = str.substring(i + 3);
            if(!cur.isEmpty() && leftover.contains(cur))return true;
        }
        return false;
    }

    public static boolean isLower(String str) {
        for (char c : str.toCharArray()) {
            if (Character.isLowerCase(c))return true;
        }
        return false;
    }

    public static boolean isUpper(String str) {
        for (char c : str.toCharArray()) {
            if (Character.isUpperCase(c))return true;
        }
        return false;
    }   

    public static boolean isDit(String str) {
        for (char c : str.toCharArray()) {
            if (Character.isDigit(c))return true;
        }
        return false;
    }    

    public static boolean isOther(String str) {
        for (char c : str.toCharArray()) {
            if (!Character.isLowerCase(c) && !Character.isUpperCase(c) && !Character.isDigit(c))return true;
        }
        return false;
    }
}

全部评论

相关推荐

10-03 17:08
已编辑
西安电子科技大学 Java
点赞 评论 收藏
分享
迷茫的大四🐶:现在是幻想时间查看图片
双非本科的出路是什么?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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