demo需求:
随机生成6位验证码,由26个大写字母和0-9数字混搭,其中每位出现数字或字母的概率必须一样
demo主要技术:
new Random().nextInt(n)的运用,该方法返回0-n(前包后不包)的随机数
demo主要代码展示:
private static String[] alphabet = { "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" };
/**
* show 方法简介
* 生成6位验证码
* @author 叶灬黎
* @return 6位验证码
*/
public static String getVerCode() {
StringBuffer code = new StringBuffer();
Random random = new Random();
int temp;
for (int i = 0; i < 6; i++) {
if (random.nextInt(2) == 0) {
temp = random.nextInt(10);
code.append(temp);
} else {
temp = random.nextInt(26);
code.append(alphabet[temp]);
}
}
return code.toString();
}
demo资源位置:
svn://106.15.229.200/Javaweb/tinyDemo_ver_code,用户 temp/temp