JAVA案例:生成验证码

package com.itheima.anli;

import java.util.Random;

public class anli2 {
    public static void main(String[] args) {
        System.out.println(createCode(4));
    }
    public static String createCode(int n){
        Random random=new Random();
        String code="";
        for (int i = 1; i <= n; i++) {
            int type=random.nextInt(3);//产生0 1 2之间的数字
            switch(type){
                case 0:
                    code +=random.nextInt(10);
                    break;
                case 1:
                    //A:65  Z:65+25
                    char a=(char)(random.nextInt(26)+65);
                    code+=a;
                    break;
                case 2:
                    //a:97 z:97+25
                    char b=(char)(random.nextInt(97)+26);
                    code+=b;
                    break;

            }
        }
        return code;
    }
}

 

posted @ 2024-03-03 20:26  小彭先森  阅读(8)  评论(0编辑  收藏  举报