仅代表个人想法
public String getCode(int num) {
Random random = new Random();
int tem;//暂存随机数字
int type;//代表类型,0为数字,1为大写字母,2为小写字母
char c;//存放ascll字符
String code = "";
for (int i = 0; i < num; i++) {
type = random.nextInt(3);
switch (type) {
case 0:
tem = random.nextInt(10);
code = code + Integer.toString(tem);
break;
case 1:
tem = random.nextInt(25) + 65;
c = (char) tem;
code = code + c;
break;
case 2:
tem = random.nextInt(25) + 97;
c = (char) tem;
code = code + c;
}
}
return code;
}