28:密码验证合格

题目描述

密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有相同长度超2的子串重复 

说明:长度超过2的子串

输入描述:一组或多组长度超过2的子符串。每组占一行

输出描述:如果符合要求输出:OK,否则输出NG

输入例子:

021Abc9000

021Abc9Abc1

021ABC9000

021$bc9000

输出例子:

OK

NG

NG

OK

  1 package prctice01;
  2 
  3 import java.util.Scanner;
  4 
  5 /*题目描述
  6 密码要求:
  7 1.长度超过8位
  8 2.包括大小写字母.数字.其它符号,以上四种至少三种
  9 3.不能有相同长度超2的子串重复 
 10 说明:长度超过2的子串
 11 输入描述:一组或多组长度超过2的子符串。每组占一行
 12 输出描述:如果符合要求输出:OK,否则输出NG
 13 输入例子:
 14 021Abc9000
 15 021Abc9Abc1
 16 021ABC9000
 17 021$bc9000
 18 输出例子:
 19 OK
 20 NG
 21 NG
 22 OK*/
 23 public class PasswordCheck {
 24 /*
 25         // 3.不能有相同长度超2的子串重复
 26         public static boolean checkCharRepeat(String password){
 27             for(int i=0 ;i<password.length()-2 ;i++){
 28                 String substr1 =password.substring(i, i+3);
 29                 if (password.substring(i+1).contains(substr1))
 30                    return false;
 31             }
 32             return true;
 33         }*/
 34         public static void main(String[] args) {
 35             Scanner cin = new Scanner(System.in);
 36             while (cin.hasNextLine()) {
 37                 String psw = cin.nextLine();
 38                 if (checkLength(psw) && checkString(psw) && checkSubString(psw))
 39                     System.out.println("OK");
 40                 else
 41                     System.out.println("NG");
 42             }
 43         }
 44         public static boolean checkLength(String psw)
 45         {
 46             if(psw==null || psw.isEmpty() || psw.length()<=8 )
 47             return false;
 48             return true;
 49         }
 50         public static boolean checkString(String psw)
 51         {
 52             int num = 0, upper = 0, lower = 0, others = 0;
 53             char[] input = psw.toCharArray();
 54             for(int i = 0;i<psw.length();i++)
 55             {
 56                  if(input[i]<'9' && input[i]>'0')
 57                 {
 58                     num=1;
 59                     continue;
 60                 }
 61                  else if(input[i]<'Z' && input[i]>'A')
 62                 {
 63                     upper=1;
 64                     continue;
 65                 }
 66                 else if(input[i]<'z' && input[i]>'a')
 67                 {
 68                     lower=1;
 69                     continue;
 70                 }
 71                 else
 72                 {
 73                     others=1;
 74                     continue;
 75                 }
 76             }
 77             int total = num+upper+lower+others;
 78             return (total>=3) ? true:false;
 79         }
 80         private static boolean checkSubString(String psw)
 81         {
 82             for(int i = 0;i<psw.length()-2;i++)
 83             {
 84                 String sub = psw.substring(i,i+3);
 85                 if(psw.substring(i+1).contains(sub)){
 86                     return false;
 87                 }
 88             }
 89             return true;
 90         }
 91 }
 92 /*public boolean isContentQualified(String s){
 93         int count=0;
 94         String[] str={"[a-z]","[A-Z]","[0-9]","[^a-zA-Z0-9]"};
 95         for(int i=0;i<str.length;i++){
 96             Pattern p=Pattern.compile(str[i]);
 97             Matcher m=p.matcher(s);
 98             if(m.find())
 99                 count++;
100         }
101         return count>=3;
102          
103     }*/
原文地址:https://www.cnblogs.com/newcoder/p/5775951.html