生成随机验证码

import java.util.Random;

public class yanzhengma {
    public static void main(String[] args) {
        System.out.println(Code(5));
    }
    public static String Code(int n){
        Random r = new Random();
        //定义一个String类型变量用于记录的每位随机字符
        String code = "";
        for(int i = 1; i <= n; i++){
           int type =  r.nextInt(3);
           switch (type){
               case 0:
                   //随机一个数字字符
                   code += r.nextInt(10);
                   break;
               case 1:
                   //随机一个大写字符
                   code += (char)r.nextInt(65,91);
                   break;
               case 2:
                   //随机一个小写字符
                   code += (char)r.nextInt(97,123);
                   break;
           }
        }return code;
    }
}

运行结果如下:生成随机验证码_第1张图片

你可能感兴趣的:(java,java,开发语言)