重复概率: 1/58^4
public static String createFourCode(){
String code = “”;
//创建一个随机数对象rand
Random rand = new Random();
//循环四次,分别获取四次随机数的值
for (int i = 0; i < 4; i++) {
//将获得的随机数0 ~ 3之间的值赋值给choice变量
int choice = rand.nextInt(3);
//将choice作为switch的条件
switch (choice) {
/*
*根据获得的随机数0 ~ 3 分为四中情况
*case 0:随机产生一个数
*case 1:随机产生一个大写字母
*case 2:随机产生一个小写字母
*default:跳出分支结构
*/
case 0:
int num = rand.nextInt(10);
code = code.concat(String.valueOf(num));
break;
case 1:
int num2 = rand.nextInt(25) + 65;
char low = (char) num2;
code = code.concat(String.valueOf(low));
break;
case 2:
int num3 = rand.nextInt(25) + 97;
char high = (char) num3;
code = code.concat(String.valueOf(high));
break;
default:
break;
}
}
return code;
}