/*
生成验证码
内容:可以是小写字母,也可以是大写字母,还可以是数字
规则:
长度为5
内容中是四位字母,1位数字
其中数字只有一位,但是可以出现在任意位置*/
package test3;
import java.util.Random;
public class exercise5 {
public static void main(String[] args) {
/*
生成验证码
内容:可以是小写字母,也可以是大写字母,还可以是数字
规则:
长度为5
内容中是四位字母,1位数字
其中数字只有一位,但是可以出现在任意位置*/
//创建一个大小写字母的数组,一个数字的数组
char []letter={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char[]number={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
//随机数对象
Random r=new Random();
//数字数组随机索引
int numberIndex=r.nextInt(number.length);
//创建一个字符数组存储验证码
char[]code=new char[5];
//将最后一个索引暂时作为数字的存储
code[4]=number[numberIndex];
//将字母填充到剩下的数组中
for (int i = 0; i < 4; i++) {
int letterIndex=r.nextInt(letter.length);
code[i]=letter[letterIndex];
}
//打乱code数组的元素的顺序
for (int i = 0; i < 5; i++) {
//创建一个随机数
int num=r.nextInt(5);
char temp=code[i];
code[i]=code[num];
code[num]=temp;
}
//将字符数组code转化为string
String code1=new String(code);
System.out.println(code1);
}
}
本文提供了一种Java实现方法,用于生成长度为5的验证码,其中包含四位字母和一位数字。验证码中的数字可以在任意位置,既包括小写字母、大写字母,也包括数字。
1759

被折叠的 条评论
为什么被折叠?



