思路:
1,判断ok的情况直接输出“ok”,并continue入下一循环,否则一律输出“NG”;
2,利用正则表达式判断字符串内容;利用indexOf和lastIndexOf判断是否重复。
import java.util.*;
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()) {
String input = scan.nextLine();
int count = 0;
boolean isRepeated = false;
if (input.length() > 8) { //长度超过8
if (Pattern.compile("[0-9]").matcher(input).find()) count++;
if (Pattern.compile("[a-z]").matcher(input).find()) count++;
if (Pattern.compile("[A-Z]").matcher(input).find()) count++;
if (Pattern.compile("[\\!\\@\\#\\$\\%\\^\\&\\*]").matcher(input).find()) count++;
if (count >= 3) { //包含三种及以上符号
for (int i = 0; i < input.length() - 3; i++) {
String str = input.substring(i, i + 3);
if (input.indexOf(str) != (input.lastIndexOf(str))) { //判断子串重复
isRepeated = true;
}
}
if (!isRepeated) {
System.out.println("OK");
continue;
}
}
}
System.out.println("NG");
}
}
}