题解 | #密码验证合格程序#
密码验证合格程序
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()) { // 注意 while 处理多个 case
String str = in.nextLine();
boolean flag = false;
int flagA = 0;
int flaga = 0;
int flagN = 0;
int flagS = 0;
for(int i = 0;i < str.length();i++){
if('A' <= str.charAt(i) && str.charAt(i) <= 'Z'){
flagA = 1;
}
else if('a' <= str.charAt(i) && str.charAt(i) <= 'z'){
flaga = 1;
}
else if('0' <= str.charAt(i) && str.charAt(i) <= '9'){
flagN = 1;
}
else{
flagS = 1;
}
}
//判断是否存在至少三种不同字符
if(flagA+flaga+flagN+flagS>=3){
//判断是否长度大于8
if(str.length()>8){
//判断是否有长度大于2的子串重复
if(!strRepeat(str)){
flag = true;
}
}
}
if(flag) System.out.println("OK");
else System.out.println("NG");
}
}
//创建查找重复字符串的方法
public static boolean strRepeat(String str){
for(int i = 1 ;i<str.length()-2;i++){
if(str.substring(i).contains(str.substring(i-1,i+2))){
return true;
}
}
return false;
}
}
