java 随机生成验证码 思路

浮夸小生。
2022-05-02 / 3 评论 / 361 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年05月02日,已超过718天没有更新,若内容或图片失效,请留言反馈。

通过ASCII表生成大小写验证码

 public static void main(String[] args) {

        System.out.println( createCode(6));
    }
    public static String createCode(int n){ //int n 表示随机生成几位
        Random r = new Random();
        String code = "";
        for (int i = 0; i < n; i++) {
            int type = r.nextInt(3);
            switch (type){
                case 0:  //大写字母 ASCII码
                  char ch = (char)(r.nextInt(25)+65);
                    code += ch;
                    break;
                case 1: //小写字母 ASCII码
                    char ch1 = (char)(r.nextInt(25)+97);
                    code += ch1;
                    break;
                case 2: //数字
                    code += r.nextInt(10);
                    break;
            }
        }
        return code ;
    }
}
0

评论 (3)

取消
  1. 头像
    鸟叔
    Windows 7 · Google Chrome

    没看懂用在哪里的验证码

    回复
    1. 头像
      浮夸小生。 作者
      Windows 10 · Google Chrome
      @ 鸟叔

      验证码生成的一个方式,只是没进行画图生成图片。

      回复
  2. 头像
    淄博漏水检测
    Windows 10 · Google Chrome

    感谢分享,赞一个

    回复