一般来说一个比较安全的密码至少应该满足下面两个条件: 
(1).密码长度大于等于8,且不要超过16。 
(2).密码中的字符应该来自下面“字符类别”中四组中的至少三组。 
这四个字符类别分别为: 
1.大写字母:A,B,C...Z; 
2.小写字母:a,b,c...z; 
3.数字:0,1,2...9; 
4.特殊符号:~,!,@,#,$,%,^; 
给你一个密码,你的任务就是判断它是不是一个安全的密码。 输入
输入数据第一行包含一个数M,接下有M行,每行一个密码(长度最大可能为50),密码仅包括上面的四类字符。输出
对于每个测试实例,判断这个密码是不是一个安全的密码,是的话输出YES,否则输出NO。样例输入
3
a1b2c3d4
Linle@ACM
^~^@^@!%
样例输出
NO
YES
NOimport java.util.*;
public class ad13 {
public static void main(String[] args) {
Scanner rd=new Scanner(System.in);
int n=rd.nextInt();
String arr[]=new String[n];
for(int i=0;i<arr.length;i++)
{
arr[i]=rd.nextLine();
if(check(arr[i])==true)
{
System.out.println("YES");
}
else
System.out.println("NO");
}
}
public static boolean check(String str)
{
String regex1=".*[0-9].*";
String regex2=".*[A-Z].*";
String regex3=".*[a-z].*";
String regex4=".*[~!@#$%^].*";
if(str!=null&&str.length()>=8&&str.length()<=16)
{
if((str.matches(regex1)&&str.matches(regex2)&&str.matches(regex3))||(str.matches(regex1)&&str.matches(regex2)&&str.matches(regex4))||(str.matches(regex1)&&str.matches(regex3)&&str.matches(regex4))||(str.matches(regex2)&&str.matches(regex3)&&str.matches(regex4)||(str.matches(regex1)&&str.matches(regex2)&&str.matches(regex3)&&str.matches(regex4))))
{
return true;
}
}
return false;
}
} 不太懂正则。。应该要怎样写~~这样太乱 而且答案不对

解决方案 »

  1.   

    ^[A-Za-z0-9/~/!/@/#/$/%/^]{1,16}$
    这是ASP页面的正则表达式
    这样的验证只满足条件一,不满足条件二。
    单纯的正则好像实现这两个条件比较麻烦,建议
    逻辑验证和正则结合起来用啊。用正则的目的是为了验证方便,如果不方便了就应该结合起来用啊。
    正则验证条件一,然后逻辑验证条件二应该就可以了。
      

  2.   

    下面的代码 我试了下 基本满足了 需求,但是有一个条件可能会有出入就是 密码 来自至少那三个条件
    你可以试着 用s.contains(...); 实在不想想了   希望楼主自己尝试解决下。
    说实话  真心蛋疼。我现在正在学JAVA  头都想着疼。MD
    import java.util.Scanner;public class SafePwd {

    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入安全密码:");
    String rule="[!~@#$%^]{0,}\\w{0,}[!~@#$%^]{0,}\\w{0,}[!~@#$%^]{0,}\\w{0,}[!~@#$%^]{0,}\\w{0,}[!~@#$%^]{0,}\\w{0,}[!~@#$%^]{0,}\\w{0,}[!~@#$%^]{0,}\\w{0,}[!~@#$%^]{0,}\\w{0,}";
    while(true){
    String s=sc.nextLine();
    if(s.matches(rule) && s.length()>=8 && s.length() <=16){
    System.out.println("YES");
    break;
    }else{
    System.out.println("No");
    continue;
    }
    }
    }
    }
      

  3.   

    [A-Za-z0-9~!@#$%^]{8,16} 
    前面[]之内表示字符的范围在A-Z或a-z或0-9或~!@#$%^字符之间
    后面{8,16}表示[]内字符出现的次数在8.16之间
      

  4.   

    import java.util.Arrays;
    import java.util.Scanner;public class RegexNotdo {
     public static void main(String[] args) {
            Scanner rd=new Scanner(System.in);
            int n=Integer.parseInt(rd.nextLine());
            String arr[]=new String[n];
            for(int i=0;i<arr.length;i++)
            {
                arr[i]=rd.nextLine();
                if(check(arr[i])==true)
                {
                    System.out.println("YES");
                }
                else
                    System.out.println("NO");
            }
        }
        public static boolean check(String str)
        {
         int count=0;
         char[] comma={'~','!','@','#','$','%','^'};
         char[] c=str.toCharArray();
         for(char temp:c){
         if(Character.isDigit(temp))
         count++;
         if(Character.isUpperCase(temp))
         count++;
         if(Character.isLowerCase(temp))
         count++;
         if(Arrays.asList(comma).contains(temp))
         count++;
         }
         if(count>=3&&c.length>=8&&c.length<=16)
          return true;
            return false;
        }
    }不用正则的方法
      

  5.   

    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;public class RegexNotdo {
     public static void main(String[] args) {
            Scanner rd=new Scanner(System.in);
            int n=Integer.parseInt(rd.nextLine());
            String arr[]=new String[n];
            for(int i=0;i<arr.length;i++)
            {
                arr[i]=rd.nextLine();
                if(check(arr[i])==true)
                {
                    System.out.println("YES");
                }
                else
                    System.out.println("NO");
            }
        }
        public static boolean check(String str)
        {
         List<Character> l=Arrays.asList('~','!','@','#','$','%','^');
         char[] c=str.toCharArray();
         boolean[] flag={false,false,false,false}; 
         for(char temp:c){
         if(Character.isDigit(temp))
         flag[0]=true;
         if(Character.isUpperCase(temp))
         flag[1]=true;
         if(Character.isLowerCase(temp))
         flag[2]=true;
         if(l.contains(temp))
         flag[3]=true;
         }
         int count=0;
         for(boolean temp:flag){
         if(temp==true)
         count++;
         }
         if(count>=3&&c.length>=8&&c.length<=16)
          return true;
            return false;
        }
    }刚刚那个理解错了,再写一个
      

  6.   

    谢了~但是check方法不是很理解,能解释下吗?
      

  7.   


    private static String check(String str) {
    if (str == null || str.length() < 8 || str.length() > 16) {
    return "NO";
    }
    String[] regexs = { "[a-z]", "[A-Z]", "\\d", "[~!@#$%^]" };
    int sign = 0;
    for (String regex : regexs) {
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(str);
    if (m.find()) {
    sign++;
    }
    }
    if (sign >= 3)
    return "OK";
    return "NO";
    }
    这样?
      

  8.   

    就是逐个字符的进行判断,然后flag数组中进行四种符号的标记,有的就置为true,然后最后统计flag中变true的数目,不过觉得13楼的写法更简单