码迷,mamicode.com
首页 > 其他好文 > 详细

1-20密码验证合格程序

时间:2017-03-17 14:09:11      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:密码   system   否则   system.in   static   第一个   import   while   als   

题目描述

密码要求: 

1.长度超过8位 

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

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

说明:长度超过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.hasNextLine()){
   int count=0;
   boolean flag=false,flag1=false;
  String s =sc.nextLine();
  if(s.length()>8){
   for(int i=0;i<s.length();i++){
    if(s.charAt(i)>=‘0‘&&s.charAt(i)<=‘9‘){
     count++;
        break;
    }
    else continue;
    }
   for(int i=0;i<s.length();i++){
    if(s.charAt(i)>=‘A‘&&s.charAt(i)<=‘Z‘){
     count++;
        break;
    }
    else continue;
    }
   for(int i=0;i<s.length();i++){
    if(s.charAt(i)>=‘a‘&&s.charAt(i)<=‘z‘){
     count++;
        break;}
    else continue;
    }
   for(int i=0;i<s.length();i++){
    if(!((s.charAt(i)>=‘0‘&&s.charAt(i)<=‘9‘)||(s.charAt(i)>=‘A‘&&s.charAt(i)<=‘Z‘)||(s.charAt(i)>=‘a‘&&s.charAt(i)<=‘z‘))){
     count++;
        break;
    }
    else continue;
    }
    if(count>=3){
     flag=true;
     while(flag){
      for(int i=0;i<=s.length()-4;i++){
       String a=s.substring(i, i+3);
       for(int j=i+1;j<=s.length()-3;j++){
        String b=s.substring(j, j+3);
        if(a.equals(b)){
            flag1=true;
            break;
        }
        else continue;
       }
       if(flag1){
        System.out.println("NG");
        flag=false;
        break;
       }
       else continue;
      }
      if(flag1==false){
        System.out.println("OK");
           flag=false;
      }
      
     }
    }
   else System.out.println("NG");
   
  }
  else System.out.println("NG");
  
  }
 }
}

收获:(1)允许输入多行:int count=0;最好放在while(sc.hasNextLine())后面,否则会记录count上次运行的值。
(2)截取字符串 String a=s.substring(i, i+3);
                 public String substring(int beginIndex, int endIndex)
                第一个int为开始的索引,对应String数字中的开始位置,
                第二个是截止的索引位置,对应String中的结束位置
               1、取得的字符串长度为:endIndex - beginIndex;
               2、从beginIndex开始取,到endIndex结束,从0开始数,其中不包括endIndex位置的字符(注意)。
               (3)比较字符串是否相等,错误写法if(a==b)
                 正确写法if(a.equals(b))

1-20密码验证合格程序

标签:密码   system   否则   system.in   static   第一个   import   while   als   

原文地址:http://www.cnblogs.com/code666/p/6565611.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!