题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
String str = sc.nextLine();
//若是满足题目要求的三个条件则输出OK,否则输出NG
if(ifrules(str)+iflength(str)+unrept(str)==3)
System.out.println("OK");
else System.out.println("NG");
}
}
//规则一,长度达标返回1,否则0
public static int iflength(String str){
if(str.length()<=8)return 0;
else return 1;
}
//规则二,符合三种以及以上种符号返回1,否则返回0
public static int ifrules(String str){
int rule1 = 0,rule2 = 0,rule3 = 0,rule4 = 0;
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i)>=48&&str.charAt(i)<=57)rule1=1;
else if(str.charAt(i)>=65&&str.charAt(i)<=90)rule2=1;
else if(str.charAt(i)>=97&&str.charAt(i)<=122)rule3=1;
else rule4=1;
}
if(rule1+rule2+rule3+rule4>=3)return 1;
else return 0;
}
//规则三,没有三个以及以上子串重复返回1,否则0
public static int unrept(String str){
String[] starr = new String[str.length()];
for (int i = 0; i < str.length()-2; i++) {
char[] temp = new char[3];
temp[0] = str.charAt(i);
temp[1] = str.charAt(i + 1);
temp[2] = str.charAt(i + 2);
String strtemp = new String(temp);
if(i==0) {
starr[i] = strtemp;
}
else{
for (int j = 0; j < i; j++) {
if(starr[j].equals(strtemp)){
return 0;
}
}
starr[i]=strtemp;
}
}
return 1;
}
}