JavaJava随机生成验证码
Beyond随机生成验证码
前两位为大写字母,后两位为小写字母,最后一位为数字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import java.util.Random;
public class Main { public static void main(String[] args) { System.out.println("随机输出5个验证码"); for(int i=1;i<=5;i++) { System.out.println(verificationCode()); } } public static String verificationCode(){ Random random = new Random(); String code=""; for(int i=1;i<=2;i++){ code+=(char)(random.nextInt(26)+65); } for(int i=1;i<=2;i++){ code+=(char)(random.nextInt(26)+97); } code+=random.nextInt(10); return code; }
}
|