您的位置:首页 > 编程语言 > Java开发

密码验证合格程序

2016-09-01 20:06 183 查看
密码要求:   1.长度超过8位   2.包括大小写字母.数字.其它符号,以上四种至少三种   3.不能有相同长度超2的子串重复import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String s;
while(scanner.hasNext()){
s=scanner.next();
if(s.length()>8&&hasNoRepeat(s)&&hasThree(s)){
System.out.println("OK");
}else{
System.out.println("NG");
}
}
}

public static boolean hasNoRepeat(String s){
String temp;
for(int i=0;i<s.length();i++)
for(int j=i+2;j<s.length();j++){
temp=s.substring(i,j);
if(s.indexOf(temp, j+1)!=-1){
return false;
}
}
return true;
}

public static boolean hasThree(String s){
int count=0;
boolean hasNum=false;
boolean hasLowChar=false;
boolean hasHighChar=false;
boolean hasOther=false;
char c;
for(int i=0;i<s.length();i++){
c=s.charAt(i);
if('0'<=c&&c<='9'){
if(!hasNum){
hasNum=true;
count++;
}
}else if('a'<=c&&c<='z'){
if(!hasLowChar){
hasLowChar=true;
count++;
}
}else if('A'<=c&&c<='Z'){
if(!hasHighChar){
if(!hasHighChar){
hasHighChar=true;
count++;
}
}
}else{
if(!hasOther){
hasOther=true;
count++;
}
}
if(count>=3){
return true;
}

}
return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java