开发验证码
需求:定义方法实现随机产生一个5位的前四位是大写字母或小写字母,最后一位是数字的验证码
public class Student {
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 number = r.nextInt(10);
result = result + number;
System.out.println(result);
}
}
该代码运行结果位随机的五位验证码