Java根据长度生成随机字母和数字源代码
 public String getCharacterAndNumber(int length)  
 {  
     String val = "";  
           
     Random random = new Random();  
     for(int i = 0; i < length; i++)  
     {  
         String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 输出字母还是数字  
               
        if("char".equalsIgnoreCase(charOrNum)) // 字符串  
        {  
             int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; //取得大写字母还是小写字母  
             val += (char) (choice + random.nextInt(26));  
         }  
         else if("num".equalsIgnoreCase(charOrNum)) // 数字  
         {  
             val += String.valueOf(random.nextInt(10));  
         }  
     }  
           
     return val;  
 }
注:1.只想要大写的字母 可以使 int choice =65; 只想要小写的字母,就 int choice =97;
2.方法的参数 length 是生成的随机数的长度。
                    
                
                
            
        
浙公网安备 33010602011771号