实现:
1) 密码至少8位
2) 包括大小写字母,数字,其他字符中的至少3种
3) 没有3个字符重复出现
4) 满足上述条件输出OK,否则NG
5) 使用正则表达式
package practic; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class T1_M2 { 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; } }