package com.itheima.test;
import java.util.Random;
public class Test3 {
public static void main(String[] args) {
//开发验证码
String code=createCode(5);
System.out.println("随机验证码:" + code);
}
public static String createCode(int n){
String code="";
Random r=new Random();
for (int i = 0; i < n; i++) {
//生成随机数(1,2,3)
int type=r.nextInt(3)+1;
switch (type){
case 1:
//大写字符(65-65+25)
char ch1=(char)(r.nextInt(26)+65);
code+=ch1;
break;
case 2:
//小写字符
char ch2=(char)(r.nextInt(26)+97);
code+=ch2;
break;
case 3:
//数字字符
code+=r.nextInt(10);
break;
}
}
return code;
}
}
【Java】实现随机产生验证码(大写小写字母,数字)
最新推荐文章于 2023-04-18 18:34:26 发布