JAVA生成四位数的验证码
2016-03-31 本文已影响1977人
ALAI丶
public class Test {
public static void main(String[] args) {
intauthCode = makeAuthCode();
System.out.println(authCode);
System.out.println("-----------------------------------");
}
public static int makeAuthCode() {
int authCodeNew =0;
authCodeNew = (int) Math.round(Math.random() * (9999-1000) +1000);
return authCodeNew;
}
}
比较简单,有兴趣的可以尝试
下面是commons-lang3下的一个工具类,4表示几位的验证码,0-9表示验证码从这些数字生成
public static String generateVerificationCode() {
return RandomStringUtils.random(4, "0123456789");
}
这个大家可以了解一下,下面是我贴的RandomStringUtils.random()的源代码



