验证密码合格程序
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG
示例1
输入
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出
OK
NG
NG
OK
import java.util.Scanner;
public class PasswordAuthentification {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String password = sc.nextLine();
if (checkLength(password) && checkCharKinds(password) && checkCharRepeat(password)) {
System.out.println("OK");
} else {
System.out.println("NG");
}
}
}
//检查长度是否合格
public static boolean checkLength(String password) {
if (password == null || password.length() <= 8) {
return false;
}
return true;
}
//检查是否包含大小写字母,数字,其它符号,中的至少三种
public static boolean checkCharKinds(String password) {
int uppercase = 0;
int lowercase = 0;
int digit = 0;
int others = 0;
char[] ch = password.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (ch[i] >= '0' && ch[i] <= '9') {
digit = 1;
continue;
}
if (ch[i] >= 'a' && ch[i] <= 'z') {
lowercase = 1;
continue;
}
if (ch[i] >= 'A' && ch[i] <= 'Z') {
uppercase = 1;
continue;
} else {
others = 1;
continue;
}
}
int total = digit + lowercase + uppercase + others;
return total >= 3 ? true : false;
}
//检查是否有不同长度超过2的子字符串重复
public static boolean checkCharRepeat(String password){
for (int i = 0; i < password.length()-2; i++) {
String substr1 = password.substring(i,i+3);
if (password.substring(i+1).contains(substr1)){
return false;
}
}
return true;
}
}