华为机试-密码验证合格程序(JAVA)

题目描述

密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度大于2的子串重复

输入描述

一组或多组长度超过2的字符串。每组占一行

输出描述

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

示例

输入:

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

输出:

OK
NG
NG
OK

代码

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            boolean flag = true;
            String str = sc.nextLine();
            char[] ch = str.toCharArray();        //转字符数组
            
            if(ch.length<=8)flag=false;    //要求1
            //&Z@#+dN0&Z
            int[] symbol = new int[]{0,0,0,0};        //定义容量为4的整型数组,分别对应小写,大写,数字,其它符号
            for(int i=0; i<ch.length; i++){        //循环,找出每个字符所属种类
                if(ch[i]>='A' && ch[i]<='Z')symbol[0]++;
                else if(ch[i]>='a' && ch[i]<='z')symbol[1]++;
                else if(ch[i]>='0' && ch[i]<='9')symbol[2]++;
                else symbol[3]++;
            }
            int count =0;            //统计字符种类
            for(int i=0; i<symbol.length; i++){
                if(symbol[i] > 0)count++;
            }
            
            if(count < 3)flag = false;        //要求2
            
            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)){
                    
                    flag = false;            //要求3
                    
                }
            }
            if(flag == true)System.out.println("OK");
            else System.out.println("NG");
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值