题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()){ String s = sc.nextLine(); if(validPassword(s)){ System.out.println("OK"); }else{ System.out.println("NG"); } } } private static boolean validPassword(String s){ if(s.length() <= 8){ return false; } int fourCondition = 0; boolean hasUpper = false; boolean hasLower = false; boolean hasDigit = false; boolean hasOther = false; for (char ch : s.toCharArray()) { if (Character.isUpperCase(ch)) { hasUpper = true; } else if (Character.isLowerCase(ch)) { hasLower = true; } else if (Character.isDigit(ch)) { hasDigit = true; } else if (ch >= 33 && ch <= 126) { hasOther = true; } } if (!(hasUpper && hasLower && hasDigit) && !(hasUpper && hasLower && hasOther) && !(hasUpper && hasDigit && hasOther) && !(hasLower && hasDigit && hasOther)) { return false; } for (int i = 0; i < s.length() - 2; i++) { String substr = s.substring(i, i + 3); if (s.indexOf(substr, i + 3) != -1) { return false; } } return true; } }