Files
thewindminer/src/main/java/com/jxy/windminer/common/utils/CodeUtils.java
2026-01-15 17:29:12 +08:00

44 lines
1.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.jxy.windminer.common.utils;
import java.util.Random;
/**
* @Description TODO
* @Date 2022/11/18 14:26
* @Author 杜懿
*/
public class CodeUtils {
//public static void main(String[] args) {
// String s = creatCode(4);
// //System.out.println("随机验证码为:" + s);
//}
//定义一个方法返回一个随机验证码
public static String creatCode(int n) {
String code = "";
Random r = new Random();
//2.在方法内部使用for循环生成指定位数的随机字符并连接起来
for (int i = 0; i <= n; i++) {
//生成一个随机字符:大写 ,小写 数字0 1 2
int type = r.nextInt(3);
switch (type) {
case 0:
char ch = (char) (r.nextInt(26) + 65);
code += ch;
break;
case 1:
char ch1 = (char) (r.nextInt(26) + 97);
code += ch1;
break;
case 2:
code += r.nextInt(10);
break;
}
}
return code;
}
}