Java 如何制作密码程序

Java 如何制作密码程序,java,passwords,Java,Passwords,我正在尝试创建一个程序,要求用户输入一个密码,条件是至少有10个字母字符和至少2个数字。 到目前为止,这是我想到的,但是程序可以读取任意数量的字母或数字作为有效密码 package password; import java.util.Scanner; public class Password { /** * @param args the command line arguments */ public static void main(String[

我正在尝试创建一个程序,要求用户输入一个密码,条件是至少有10个字母字符和至少2个数字。 到目前为止,这是我想到的,但是程序可以读取任意数量的字母或数字作为有效密码

package password;
import java.util.Scanner;

public class Password {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       String password;
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Enter a password with 10 characters total with "+
                "2 numbers: ");
        password=keyboard.nextLine();



        System.exit(0);


    }

    public static boolean passwordCheck(String password)

   {
       if (password.length() <10) return false ;

               int letterCount = 0;
               int numCount =0;
               for (int i =0; i < password.length (); i++ )
               {char ch = password.charAt(i);

               if (letter(ch)) letterCount++; 
               else if (numeric(ch)) numCount++;
               else return false; 

               }   

    return (letterCount >= 2 && numCount >= 2);
            } 
    public static boolean letter(char ch)
    {
         return (ch >= 'a' && ch <= 'z') ||
           (ch >= 'A' && ch <= 'Z') ;
    }


    public static boolean numeric(char ch)
    {
        return(ch >= '0' && ch <= '9');
    }


}
包密码;
导入java.util.Scanner;
公共类密码{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
字符串密码;
扫描仪键盘=新扫描仪(System.in);
System.out.println(“输入总共10个字符的密码”+
“2个数字:”;
密码=键盘.nextLine();
系统出口(0);
}
公共静态布尔密码检查(字符串密码)
{
if(password.length()=2&&numCount>=2);
} 
公共静态布尔字母(char ch)
{
return(ch>='a'&&ch='a'&&ch='0'&&ch试试这个

     if (password.length() < 10) {   
        return false;  
    } else {      
        char c;  
        int count = 1;   
        for (int i = 1; i < password.length(); i++) {  
            c = password.charAt(i);  
            if (!Character.isLetterOrDigit(c)) {          
                return false;  
            } else if (Character.isDigit(c)) {  
                count++;  
                if (count < 2)   {     
                    return false;  
                }     
            }  
        }  
    }  
    return true;  
    }
if(password.length()<10){
返回false;
}否则{
字符c;
整数计数=1;
对于(inti=1;i