直接上代码
/**
* 验证码工具类,根据你输入的位数随机生成
*/
public static String createCode(int n) {
Random random = new Random();
String a = "";
for (int i = 1; i <= n; i++) {
int type = random.nextInt(3);
switch (type) {
case 0:
a += random.nextInt(10);
break;
case 1:
a += (char)(random.nextInt(26) + 65);
break;
case 2:
a += (char)(random.nextInt(26) + 97);
break;
}
}
return a;
}
结果