随机生成指定长度的密码
作者:互联网
代码分析
判断字符是否为空,空默认字符长度6,否则根据参数长度设置
字符串,我们需要在这里面随机获取数据
实例化随机类
实例化StringBuilder
for循环根据设置的长度,来给StringBuilder赋值
在str字符串中随机获取数据附加给StringBuilder
返回StringBuilder
/**
* @author Mxhlin
* @Email fuhua277@163.com
* @Date 2022/09/15/20:19
* @Version
* @Description 随机生成指定长度的密码
*/
public class RandomPassword {
/**
* @Description: 生成密码
* @Param: [n]
* @return: java.lang.String
* @Author: Mxhlin
* @Date: 2022/9/15
*/
public String getPassword(int... n) {
int num = n.length == 0 ? 6 : n[0];
String str = "0123456789_-=+'\",`~!@#$%^&* ()abcdefghijklmnolpqrstuvwxyzABCDEFGHIJKLMNOLPQRSTUVWXYZ";
Random ra = new Random();
StringBuilder sb = new StringBuilder(6);
for (int i = 0; i < num; i++) {
sb.append(str.charAt(ra.nextInt(str.length())));
}
return sb.toString();
}
/**
* @Description: 校验码
* @Param: [n]
* @return: java.lang.String
* @Author: Mxhlin
* @Date: 2022/9/15
*/
public String checkcode(int n){
String str ="0123456789abcdefghijklmnopqrstuzwsyzABCDEFGHIJKLMNOPQRSTUZWSYZ";
Random random = new Random();
StringBuilder sb = new StringBuilder(6);
for (int i = 0; i < n; i++) {
sb.append(str.charAt(random.nextInt(str.length())));
}
return sb.toString();
}
}
标签:return,String,int,StringBuilder,密码,随机,str,sb,长度 来源: https://www.cnblogs.com/xhlin/p/16698069.html