public class VerificationCode {
public static void main(String[] args) {
//1.随机函数
Random random = new Random();
//2.定义字符串变量用于存放字符
String str = "";
//3.使用for循环来控制验证码的长度
for (int i = 0; i < 4; i++) {
switch (random.nextInt(3)+1){
case 1:
str += (char)(random.nextInt(10)+48);//0-9的随机数
break;
case 2:
str += (char)(random.nextInt(26)+65);//A-Z的随机数
break;
case 3:
str += (char)(random.nextInt(26)+97);//a-z的随机数
break;
}
}
System.out.println(str);
}
}
随机生成四位数验证码,包含1-9,a-z,A-Z
于 2024-03-16 14:56:09 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)