java基础模拟验证码功能

  • Post author:
  • Post category:java


模拟:生成一个随机的验证码    内容:可能有小写字母、大写字母、数字


代码如下:👇


此程序只是模拟!!!

public class test3_2 {
    public static void main(String[] args) {
        //模拟:生成一个随机的验证码
        //内容:可能有小写字母、大写字母、数字

        //1.定义字符数组
        String[] str = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
                "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
                "0","1","2","3","4","5","6","7","8","9"};

        //2.生成随机数
        Random random = new Random();
        //定义一个数组 长度为6  定义验证码的长度
        String[] newStr = new String[6];//验证码长度是6
        //循环6次取数
        for (int i = 0; i < 6; i++) {
            //随机数的大小一定要是数组长度的范围才可
            int StrNumber = random.nextInt(str.length);
            //用每次获取的随机数当数组的索引,并添加到新的数组中
            newStr[i] = str[StrNumber];
        }

        //用StringBuilder 来存储数组里面的数据并转换为字符串
        StringBuilder sb = new StringBuilder();

        //for遍历
        for (int i = 0; i < newStr.length; i++) {
            sb.append(newStr[i]);
        }
        //转换为字符串
        String s = sb.toString();
        System.out.println("随机生成的验证码为:"+s);
    }
}

运行结果:


模拟验证码功能进阶版:👇

public class test3_2_1 {
    public static void main(String[] args) {
        //死循环
        while (true){
            //获取验证码
            String verification = verification();
            System.out.println("验证码是:"+verification);

            //键盘录入
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入验证码->");
            String str = sc.next();

            //比对生成的验证码与输入的验证码是否一致
            if (str.equals(verification)){
                System.out.println("验证码输入正确√");
                //输入正确就跳出循环
                break;
            }else {
                System.out.println("验证码输入错误× 请重新输入!");
                //continue;
            }
        }
    }
    public static String verification(){
        //模拟:生成一个随机的验证码
        //内容:可能有小写字母、大写字母、数字

        //1.定义字符数组
        String[] str = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
                "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
                "0","1","2","3","4","5","6","7","8","9"};

        //2.生成随机数
        Random random = new Random();
        //定义一个数组 长度为6  定义验证码的长度
        String[] newStr = new String[6];//验证码长度是6
        //循环6次取数
        for (int i = 0; i < 6; i++) {
            //随机数的大小一定要是数组长度的范围才可
            int StrNumber = random.nextInt(str.length);
            //用每次获取的随机数当数组的索引,并添加到新的数组中
            newStr[i] = str[StrNumber];
        }

        //用StringBuilder 来存储数组里面的数据并转换为字符串
        StringBuilder sb = new StringBuilder();

        //for遍历
        for (int i = 0; i < newStr.length; i++) {
            sb.append(newStr[i]);
        }
        //转换为字符串
        String s = sb.toString();
//        System.out.println("随机生成的验证码为:"+s);
        return s;
    }
}



版权声明:本文为weixin_57595520原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。