该验证码实现随机产生一个5位的验证码,前4位为大小写字母,第5位是数字.
import java.util.Random;
public class Demo {
public static void main(String[] args) {
//将字母转化为字符串
char[]chs=new char[52];
for (int i = 0; i < chs.length; i++) {
if(i<=25){
chs[i]=(char)(97+i);
}else{
chs[i]=(char)(65+i-26);
}
}
String result=" ";
Random r=new Random();
for (int i = 0; i < 4; i++) {
int randomIndex=r.nextInt(chs.length);
result=result+chs[randomIndex];
}
int num=r.nextInt(10);//定义第5位数字是1~10之间的数字,且不含有10
result=result+num;
System.out.println(result);
}
}