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

Java-NowCoder-密码验证合格程序

2016-09-23 22:32 513 查看
题目描述:密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串
输入描述:一组或多组长度超过2的子符串。每组占一行
输出描述:如果符合要求输出:OK,否则输出NG
输入例子:
021Abc9000

021Abc9Abc1

021ABC9000

021$bc9000

输出例子:
OK

NG

NG

OK

import java.util.Scanner;
//密码要求:
/*
* 1.长度超过8位
* 2.包括大小写字母.数字.其它符号,以上四种至少三种
* 3.不能有相同长度超2的子串重复
* 说明:长度超过2的子串
* */
public class NC_020_密码验证合格程序 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String pwd = sc.nextLine();
if(pwd != null && pwd.length() > 8 && format(pwd) && checkSub(pwd)){//合法性判断
System.out.println("OK");

}else{
System.out.println("NG");
}
}
}
//验证字符串的长度至少为三种字符以上的格式 format_num
public static boolean format(String str){
int format_num_a = 0;//'a'--'z'
int format_num_A = 0;//'A'--'Z'
int format_num_0 = 0;//'0'--'9'
int format_num_other = 0;//其他字符

for(int i = 0; i < str.length(); i++){
if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z'){
format_num_a = 1;
continue;
}else if(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z'){
format_num_A = 1;
continue;
}else if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
format_num_0 = 1;
continue;
}else{
format_num_other = 1;
continue;
}
}
int format_total = format_num_0 + format_num_a + format_num_A + format_num_other;
//System.out.println(format_total);
return format_total >= 3 ? true : false;
}

//不能有相同子串超过2的子串重复
public static boolean checkSub(String str){
boolean check_sub = true;//默认子串不含有2个字符重复的
for(int i = 0; i < str.length() - 2; i++){
String subStr = str.substring(i, i + 3);//子串是取不到i+3的位置,取他的前一位
if(str.substring(i + 1).contains(subStr)){
//System.out.println(check_sub);
check_sub = false;
}
}
return check_sub;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: