随机生成6位验证码算法(数字,大小写字母)

public class verificationCode {
    /*
    * 生成随机6位数验证码,包括数字和字母
    * 0~9  ascii:48~57
    * a~z 是 97~122
    * A~Z 是 65~90
    *
    * 1:生成一个1~3的随机数,取整,来表示随机生成     Math.floor(1+Math.random()*3)
    * 2:生成48~57的随机数    Math.floor(48+Math.random()*10)
    * 3:生成97~122的随机数   Math.floor(97+Math.random()*26)
    * 3:生成65~90的随机数    Math.floor(65+Math.random()*26)
    *
    * */
    public static String creatCode(){
        char[] code =new char[6];

        for (int i=0;i<6;i++){
            int flag =(int) Math.floor(1+Math.random()*3);
            switch (flag){
                case 1 :
                    code[i] =(char) Math.floor(48+Math.random()*10);
                    break;
                case 2:
                    code[i] = (char) Math.floor(97+Math.random()*26);
                    break;
                case 3:
                    code[i] = (char) Math.floor(65+Math.random()*26);
                    break;
            }
        }
        String codeString =new String(code);
        return codeString;
    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值