随机验证码:共五位,前四位为大小写英文字母,最后一位为数字
import java.util.Random;
public class Test {
public static void main(String[] args) {
//调用生成随机字母方法四次并且打印
System.out.println("验证码为");
for (int i = 0; i < 4; i++) {
char letter = letter();
System.out.print(letter);
}
//调用一次生成随机个位数,
int number = number();
//连接输出
System.out.println(number);
}
//定义生成随机字母的方法
public static char letter() {
char letter = 0;
Random r = new Random();
int flag = r.nextInt(0, 2);
int i;
if (flag == 0) {
i = r.nextInt(97, 122);
} else {
i = r.nextInt(65, 90);
}
letter = (char) i;
return letter;
}
//定义生成随机数字的方法
public static int number(){
Random r = new Random();
return r.nextInt(0, 9);
}
}