public class 随机验证码 {
public static void main(String[] args) {
String code = createCode(5);
System.out.println("随机验证码" + code);
}
public static String createCode(int n) {
String code = "";
Random r = new Random();
for (int i = 0; i < n; i++) {
int type = r.nextInt(3);
switch (type) {
case 0:
//大写字母 A:26 z:26+65
char ch = (char) (r.nextInt(26) + 65);
code += ch;
break;
case 1:
//小写字母 a:97 z:97+25
char ch1 = (char) (r.nextInt(26) + 97);
code += ch1;
break;
case 2:
//数字
code += r.nextInt(10);
break;
}
}
return code;
}
}