0%

生成验证码

验证码规则:

​ 长度为5

​ 由4位大写或者小写字母和1位数字组成,同一个字母可重复

​ 数字可以出现在任意位置

比如:aQa1K

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private static String getVericode() {
//1.创建一个集合添加所有的大写和小写字母
ArrayList<Character> list=new ArrayList<>();
for(int i = 0 ; i<26 ; i++){
list.add((char)('a'+i));
list.add((char)('A'+i));
}
StringBuffer sb = new StringBuffer();
Random rd = new Random();
//2.要随机抽取4个字符
for (int i = 0; i < 4; i++) {
//获取随机索引
int index=rd.nextInt(list.size());
//利用索引获取字符
char c=list.get(index);
sb.append(c);
}
//3.生成1位随机数字
int number = rd.nextInt(10); // 生成0到9之间的随机数字
sb.append(number);
//4.将字符串变成字符数组,然后再新建一个新的字符串
char[] arr=sb.toString().toCharArray();
//拿着最后一个索引,跟随机索引进行交换
int randomIndex=rd.nextInt(arr.length);
//最大索引指向的元素,跟随机索引指向的元素交换
char temp=arr[randomIndex];
arr[randomIndex]=arr[arr.length-1];
arr[arr.length-1]=temp;
return new String(arr);
}