1 public class Test01 { 2 public static void main(String[] args) { 3 String code = Test01.randomCode(32); 4 System.out.println(code); 5 } 6 7 // 返回随机字符串, 8 public static String randomCode(int length) { 9 // 定义字符串的范围 10 char[] codeSequence = { ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, 11 ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, 12 ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, 13 ‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, 14 ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, 15 ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘ }; 16 17 // 随机函数 18 Random random = new Random(); 19 20 // 用于获取到字符串后添加, 21 StringBuilder builder = new StringBuilder(); 22 23 for (int i = 0; i < length; i++) { 24 int b = random.nextInt(62); 25 builder.append(codeSequence[b]); 26 } 27 return builder.toString(); 28 } 29 }
时间: 2024-10-24 08:34:52