学习目标:
随机验证码。
随机生成十组六位字符组成的验证码。验证码由大小写字母、数字字符组成。
代码实现,效果如图所示:
开发提示:
使用字符数组保存原始字符,利用Random类生成随机索引。
代码实现:
package com.apesouce.test2;
import java.util.Random;
public class Test01 {
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 6; j++) {
int random = r.nextInt('z' - '0' + 1 )+'0';
if ((random >= '0' && random <= '9') || (random >= 'a' && random <= 'z') || (random >= 'A' && random <= 'Z')) {
char b = (char) random;
System.out.println(b);
} else {
// 阿斯克码 值 在 字符'9' 到 'a' 不连续 则也不能i++ 所有遇到此种情况 需要 i-- 抵消本次的i++
i--;
}
}
System.out.println("aaaaa");
}
}
}