1 package dx; 2 3 import java.text.DecimalFormat; 4 import java.util.Random; 5 6 public class DxL { //创建类 7 8 public static void main(String[] args) 9 { 10 String str = "1234567890abcdefghijklmnopqistuvwxyz";//定义包含所有数字字母的字符串 11 12 Random sj = new Random();//实例化一个Random类 13 14 System.out.print("生成4位验证码 = "); 15 16 for(i = 0; i < 4 ; i++) 17 { 18 int sy = sj.nextInt(35);//产生一个0-35之间的整数 19 System.out.print(str.substring(sy , sy+1));//输出截取的字符串str中索引为sy的子串 20 } 21 22 } 23 24 }
原文:http://www.cnblogs.com/dirgo/p/4871318.html