java写一个验证码

生成验证码

内容:可以是小写字母,也可以是大写字母,还可以是数字

规则

长度为5

内容中是四位字母,1位数字。

其中数字只有1位,但是可以出现在任意的位置。

java 复制代码
package User;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Test {
    public static void main(String[] args) {

        System.out.println(getcode());

    }

    private static String getcode() {
        //1.创建集合
        ArrayList<Character> list=new ArrayList<>();
        //将大小写字母放在集合list当中
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }
       //获取4个随机验证码
        StringBuffer sb=new StringBuffer();
        Random r=new Random();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            sb.append(list.get(index));

        }
        //System.out.println(sb);
        //获取一个数字
        int number = r.nextInt(10);
        sb.append(number);
        //打乱数字排序的地方
       char []arr= sb.toString().toCharArray();
        for (int i = 0; i < arr.length; i++) {
            int index = r.nextInt(arr.length);
            char tmp=arr[index];
            arr[index]=arr[i];
            arr[i]=tmp;
        }
        return new String(arr);
    }

}
相关推荐
Rain的Java大神之路17 分钟前
Spring Boot 3 升级实战:真正吃掉工期的,是 MyBatis-Plus 坐标和 Security 6 重写
java·后端·面试
泡海椒28 分钟前
Java PDF 报表开发:一页简历的两种版式风格对比与折行避坑
java·开发语言·pdf
2601_9628857230 分钟前
如何用 Python 计算终极振荡器(Ultimate Oscillator, UOS)?
开发语言·python
wuminyu35 分钟前
OpenJDK学习笔记:Native事件队列与Java回调背压
java·linux·c语言·jvm·c++
源码_空间1 小时前
基于Springboot在线考试系统(源码+文档)
java·spring boot·后端
@SmartSi1 小时前
AgentScope Java 入门 14:如何使用 PlanNotebook 让智能体学会“先规划,后执行“
java·agentscope
AI开源软件2 小时前
用MQTT跑通一台模拟设备:看懂IoT产品、物模型和告警
java·vue.js·spring boot·物联网·mqtt·iot·企业管理
benchmark_cc2 小时前
一次获取多只股票实时行情:QuantDash `quotes.get(symbols=[...])` 批量快照
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
geovindu2 小时前
python: PCA
开发语言·后端·python·算法
__万波__3 小时前
嵌入式存储技术--1.Flash物理存储
java·开发语言