美文网首页程序员
JAVA生成四位数的验证码

JAVA生成四位数的验证码

作者: ALAI丶 | 来源:发表于2016-03-31 17:14 被阅读1977次

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()的源代码


相关文章

网友评论

    本文标题:JAVA生成四位数的验证码

    本文链接:https://www.haomeiwen.com/subject/gsgelttx.html