很简单的一个小程序啊,难道要全部代码??
最简单的实现思路就
1.用pass.length计算长度是否符合
2.用for语句逐个检查
3.给所有语句套上try块,遇到以上两项检查碰到不符合的地方throw一个自己定义的exception类
这是最基本的代码,建议你还是自己手写,如果这也要别人给你代码的话
你是学不下去的

解决方案 »

  1.   

    楼上说的对啊!自己写,多简单啊,思路在你的练习题要求中已经很明显了!
    比如“必须由7个字符组成”,这句你就可以通过length来计算长度是否满足!
    “其中最少有1个数字”,这句就说明你可以通过循环语句来判断!
    “如不符合要求,抛出一个异常”,这句你就用捕捉异常来实现,要是不满足要求,你就抛出异常就行了!
      

  2.   

    不用try,只要不符合要求就直接throw new Exception("密码不符合");
      

  3.   

    你可以写个checkInput的方法来判断输入是否合法,至于exception是用于程序扑获的,最好不要通过抛出异常来控制程序的流程。
      

  4.   

    参考下面代码import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test {
        
        public static void chkPwd(String strPwd) throws Exception{
            System.out.println("the password is : " + strPwd);
            Pattern p = Pattern.compile("[0-9a-zA-Z]*[0-9][0-9a-zA-Z]*");
            Matcher m = p.matcher(strPwd);
            boolean b = m.matches();
            if(b==false || strPwd.length()!=7) 
                throw new Exception("Password is error!");
        }
        public static void main(String[] args) {
            boolean bln=false;
            while(bln == false){
                String strPwd = "";
                byte[] bytPwd = new byte[1];
                try{
                    int b;
                    while((b=System.in.read(bytPwd))>0){
                        if("\r".equals(new String(bytPwd))) continue;
                        if("\n".equals(new String(bytPwd))) break;
                        strPwd += new String(bytPwd);
                    }
                    chkPwd(strPwd);
                    bln = true;
                    
                }catch(Exception e){
                    System.out.println(e.toString());
                }
            }
            System.out.println("Password is right!");
        }
    }
      

  5.   

    谢谢各位解答,谢谢楼上的代码,不过楼上的代码有些复杂,看不太懂。这道题的思路我是有,我知道用length计算长度,用for逐个检查7个字符中是否有最少一个数字。问题是在这里,我用String input = JOptionPane.showInputDialog("Enter 7 characters for password");来读取输入,我用for怎么来判断其中有无数字,把String变成7个char吗?就是这里我不知该怎么做。
      

  6.   

    字符截取:char chatAt(int where)
    void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
      

  7.   

    感谢大家,我昨晚已经把问题解决了。有2个方法:
    1:………… 
    for(int i=0;i<s.length();i++) 
          { 
             ch=s.charAt(i); 
             if(((int)ch>47)&&((int)ch<58 )) 
                ………… 
          } 
    …………——————————————————————————————————————
    2:String input; 
    input = "fdsf2"; 
    int n = 0; 

    for(int i = 0; i < input.length(); i++)

    if(Character.isDigit(input.charAt(i)))
    n++;


    if(n == 0)
    System.out.println("There is no number in this string."); 
    else 
    System.out.println("There is number(s) in this string.");