题解 | #密码验证合格程序#
密码验证合格程序
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()) { check(in.nextLine()); } } public static void check(String str) { int[] toAbove = {0, 0, 0, 0}; boolean isLength = false;//长度是否超8位 boolean isAbove3 = false;//是否三种以上数字(字母大写,字母小写,特殊字符,数字) boolean isRepeat6 = false;//重复的字符串和<6; int repeat = 0; if (str.length() > 8) { isLength = true; } for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch > 64 && ch < 91) { //大写;65-90 ASCII小写:97-122//数字0-9 48——57;space 32 toAbove[0] = 1; } else if (ch > 96 && ch < 123) { toAbove[1] = 1; } else if (ch - '0' < 10 && ch - '0' > -1) { toAbove[2] = 1; } else if (ch != 32) { toAbove[3] = 1; } int sum = toAbove[0] + toAbove[1] + toAbove[2] + toAbove[3]; if (sum >= 3) isAbove3 = true; } isRepeat6=isOver3ReaptNoSet(str); if (isAbove3 && isLength && !isRepeat6) { System.out.println("OK"); } else { System.out.println("NG"); } isAbove3 = false; isLength = false; isRepeat6 = false; } public static boolean isOver3ReaptNoSet(String str) {//好像不能使用hashset,太坑了 for (int i = 0; i < str.length() - 3; i++) { String subStr = str.substring(i, i + 3); //aaaa for (int j = i + 1; j < str.length() - 3; j++) { String subStr1 = str.substring(j, j + 3); if (subStr.equals(subStr1)) { return true; } } } return false; } }