随机生成6位的字符串验证码,要求包含数字、大小写字母

package com.twod1z;
/**
 * @program: com.twod1z
 * @description:随机生成6位的字符串验证码,要求包含数字、大小写字母
 * @author: Mr.Lin
 * @create: 2019年7月29日
 **/

public class Two002 {
    public static String getCode(int length){
        String code = "";
        for(int i=0;i<length;i++){
            boolean boo = (int)(Math.random()*2)==0;
            if(boo){
                code += String.valueOf((int)(Math.random()*10));
            }else {
                int temp = (int)(Math.random()*2)==0?65:97;
                char ch = (char)(Math.random()*26+temp);
                code += String.valueOf(ch);
            }
        }
        return code;
    }

    public static void main(String[] args) {

        System.out.println(Two002.getCode(6));
        System.out.println("-----------------");
        System.out.println(Two002.getVerify(6));
    }

    public static String getVerify(int length){
        String code = "";
        String str = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASFGHJKLZXCVBNM";
        String[] strs = str.split("");
        for(int i = 0;i<length;i++){
            code += strs[(int)(Math.random()*strs.length)];
        }
        return code;
    }
}
随机生成6位验证码

 

猜你喜欢

转载自www.cnblogs.com/lpbk/p/11267979.html