实现:
1) 密码至少8位
2) 包括大小写字母,数字,其他字符中的至少3种
3) 没有3个字符重复出现
4) 满足上述条件输出OK,否则NG
public class T1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.length() > 7 && charRule3(s) > 2 && noRepeat3(s)) System.out.println("OK"); else System.out.println("NG"); } } public static int charRule3(String s) { char[] c = s.toCharArray(); int nn = 0; int na = 0; int nA = 0; int no = 0; for (int i = 0; i < s.length(); i++) { if (c[i] >= '0' && c[i] <= '9') nn = 1; else if (c[i] >= 'a' && c[i] <= 'z') na = 1; else if (c[i] >= 'A' && c[i] <= 'Z') nA = 1; else no = 1; } return nn + na + nA + no; } public static boolean noRepeat3(String s) { for (int i = 0; i < s.length() - 3; i++) { if (s.substring(i + 3, s.length()).contains(s.substring(i, i + 3))) return false; } return true; } }
密码复杂度检验 正则表达式
实现:
1) 密码至少8位
2) 包括大小写字母,数字,其他字符中的至少3种
3) 没有3个字符重复出现
4) 满足上述条件输出OK,否则NG
5) 使用正则表达式
密码复杂度检验 正则表达式 实现: 1) 密码至少8位 2) 包括大小写字母,数字,其他字符中的至少3种 3) 没有3个字符重复出现 4) 满足上述条件输出OK,否则NG 5) 使用正则表达式 复制代码 public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.length() > 7 && hasUpperCaseLetter(s) + hasLowerCaseLetter(s) + hasNumber(s) + hasSymbol(s) > 2 && noRepeat3(s)) System.out.println("OK"); else System.out.println("NG"); } } public static int hasUpperCaseLetter(String s) { String regexp = ".*[A-Z].*"; Pattern p = Pattern.compile(regexp); Matcher m = p.matcher(s); if (m.find()) return 1; else return 0; } public static int hasLowerCaseLetter(String s) { String regexp = ".*[a-z].*"; Pattern p = Pattern.compile(regexp); Matcher m = p.matcher(s); if (m.find()) return 1; else return 0; } public static int hasNumber(String s) { String regexp = ".*[0-9].*"; Pattern p = Pattern.compile(regexp); Matcher m = p.matcher(s); if (m.find()) return 1; else return 0; } public static int hasSymbol(String s) { String regexp = ".*[^a-zA-Z0-9].*"; Pattern p = Pattern.compile(regexp); Matcher m = p.matcher(s); if (m.find()) return 1; else return 0; } public static boolean noRepeat3(String s) { int count; for (int i = 0; i < s.length() - 4; i++) { String regexp = s.substring(i, i + 3); Pattern p = Pattern.compile(regexp); Matcher m = p.matcher(s); count = 0; while (m.find()) { if (++count == 2) return false; } } return true; }
浙公网安备 33010602011771号