public class test {
public static void main(String[] args) {
System.out.println(code(5));
}
public static String code(int n){
/***
* 生成指定位数的验证码,包括数字,小写字母,大写字母
* 返回为string类型
*/
//生成以为随机字符
Random rd = new Random();
//定义一个String类型变量来接收随机数
String re = "";
//循环遍历n位随机数
for (int i = 0; i < n; i++) {
//随机三种类型字符
int type = rd.nextInt(3);
switch (type){
case 0:
//数字
re += rd.nextInt(10);
break;
case 1:
//大写字母
char c1 = (char) (rd.nextInt(26) + 65);
re += c1;
break;
case 2:
//小写字母
char c2 = (char) (rd.nextInt(26) + 97);
re += c2;
break;
}
}
return re;
}
}
生成随机验证码(java基础)
最新推荐文章于 2024-05-16 20:42:16 发布