thewindminer 代码提交
This commit is contained in:
43
src/main/java/com/jxy/windminer/common/utils/CodeUtils.java
Normal file
43
src/main/java/com/jxy/windminer/common/utils/CodeUtils.java
Normal file
@@ -0,0 +1,43 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user