密码验证合格程序
2018-01-09 21:59
120 查看
题目描述
密码要求:1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串
输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG示例1
输入
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000
输出
OK NG NG OK
实现代码:
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(); String answer = JudgePassword(str); System.out.println(answer); } } public static String JudgePassword(String str){ int BigLetters_num = 0; int SmallLetters_num = 0; int Number_num = 0; int Other_num = 0; if(str.length()<=8){ return "NG"; } for(int i=0;i<str.length();i++){ if(str.charAt(i)<='Z' && str.charAt(i)>='A'){ BigLetters_num = 1; }else if(str.charAt(i)<='z' && str.charAt(i)>='a'){ SmallLetters_num = 1; }else if(str.charAt(i)<='9' && str.charAt(i)>='0'){ Number_num = 1; }else{ Other_num = 1; } } int num = BigLetters_num +SmallLetters_num + Number_num + Other_num; if(num < 3){ return "NG"; } for(int i=0;i<str.length()-3;i++){ String str1 = str.substring(i, i+3); String str2 = str.substring(i+3); if(str2.contains(str1)){ return "NG"; } } return "OK"; } }