import java.util.Random;
public class Demo4_1 {
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:
char ch = (char) (r.nextInt(26) + 65);
code += ch;
break;
case 1:
char ch1 = (char) (r.nextInt(26) + 97);
code += ch1;
break;
case 2:
code += r.nextInt(10);
break;
}
}
return code;
}
}
Java生成随机数(生成随机验证码)
最新推荐文章于 2024-06-03 16:07:12 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)