【Java】账户初始化密码

账户初始化密码

睡你的椒发布于 今天 02:34

public class RandomPwd {

/**

* 生成随机密码:6位数字 */ public static String randomSixDigitsPwd() {

Random random = new Random();

char[] arr = new char[6];

StringBuilder sb = new StringBuilder();

for (int i = 0; i < arr.length; i++) {

int randomInt = random.nextInt(10);

arr[i] = (char)('0' + randomInt); // much better

sb.append(randomInt);

}

System.out.println(new String(arr));

System.out.println(sb.toString());

return new String(arr);

}

public static void main(String[] args) {

}

public static String complicatedPwd() {

Random random = new Random();

char[] arr = new char[8];

arr[nextIndex(random, arr)] = nextSpecial(random);

arr[nextIndex(random, arr)] = nextUpper(random);

arr[nextIndex(random, arr)] = nextLower(random);

arr[nextIndex(random, arr)] = nextDigit(random);

for (int i = 0; i < 8; i++) {

if (arr[i] == 0) {

arr[i] = nextChar(random);

}

}

System.out.println(new String(arr));

return new String(arr);

}

public static char nextDigit(Random random) {

return (char)('0' + random.nextInt(10));

}

public static char nextUpper(Random random) {

return (char)('A' + random.nextInt(26));

}

public static char nextLower(Random random) {

return (char)('a' + random.nextInt(26));

}

public static char nextSpecial(Random random) {

return specialChars.charAt(random.nextInt(specialChars.length()));

}

public static int nextIndex(Random random, char[] arr) {

int index = random.nextInt(arr.length);

while (arr[index] != 0) {

index = random.nextInt(arr.length);

}

return index;

}

/**

* 生成随机密码:复杂8位 * 至少要含一个大写字段、一个小写字段、一个特殊符号、一个数字 */

public static char nextChar(Random random) {

switch (random.nextInt(4)) {

case 0: return (char)('A' + random.nextInt(26));

case 1: return (char)('a' + random.nextInt(26));

case 2: return (char)('0' + random.nextInt(10));

default: return specialChars.charAt(random.nextInt(specialChars.length()));

}

}

private static final String specialChars = "[email protected]#$%^&*()_+-=";

/**

* 生成随机密码:简单8位 * 字符可能由大写字母、小写字母、数字和特殊符号组织 */ public static String randomEightPwd() {

Random random = new Random();

char[] arr = new char[8];

for (int i = 0; i < arr.length; i++) {

arr[i] = nextChar(random);

}

System.out.println(new String(arr));

return new String(arr);

}

}

java

阅读 33更新于 今天 02:36

本作品系原创,采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议

avatar

睡你的椒

7 声望

0 粉丝

0 条评论

得票时间

avatar

睡你的椒

7 声望

0 粉丝

宣传栏

public class RandomPwd {

/**

* 生成随机密码:6位数字 */ public static String randomSixDigitsPwd() {

Random random = new Random();

char[] arr = new char[6];

StringBuilder sb = new StringBuilder();

for (int i = 0; i < arr.length; i++) {

int randomInt = random.nextInt(10);

arr[i] = (char)('0' + randomInt); // much better

sb.append(randomInt);

}

System.out.println(new String(arr));

System.out.println(sb.toString());

return new String(arr);

}

public static void main(String[] args) {

}

public static String complicatedPwd() {

Random random = new Random();

char[] arr = new char[8];

arr[nextIndex(random, arr)] = nextSpecial(random);

arr[nextIndex(random, arr)] = nextUpper(random);

arr[nextIndex(random, arr)] = nextLower(random);

arr[nextIndex(random, arr)] = nextDigit(random);

for (int i = 0; i < 8; i++) {

if (arr[i] == 0) {

arr[i] = nextChar(random);

}

}

System.out.println(new String(arr));

return new String(arr);

}

public static char nextDigit(Random random) {

return (char)('0' + random.nextInt(10));

}

public static char nextUpper(Random random) {

return (char)('A' + random.nextInt(26));

}

public static char nextLower(Random random) {

return (char)('a' + random.nextInt(26));

}

public static char nextSpecial(Random random) {

return specialChars.charAt(random.nextInt(specialChars.length()));

}

public static int nextIndex(Random random, char[] arr) {

int index = random.nextInt(arr.length);

while (arr[index] != 0) {

index = random.nextInt(arr.length);

}

return index;

}

/**

* 生成随机密码:复杂8位 * 至少要含一个大写字段、一个小写字段、一个特殊符号、一个数字 */

public static char nextChar(Random random) {

switch (random.nextInt(4)) {

case 0: return (char)('A' + random.nextInt(26));

case 1: return (char)('a' + random.nextInt(26));

case 2: return (char)('0' + random.nextInt(10));

default: return specialChars.charAt(random.nextInt(specialChars.length()));

}

}

private static final String specialChars = "[email protected]#$%^&*()_+-=";

/**

* 生成随机密码:简单8位 * 字符可能由大写字母、小写字母、数字和特殊符号组织 */ public static String randomEightPwd() {

Random random = new Random();

char[] arr = new char[8];

for (int i = 0; i < arr.length; i++) {

arr[i] = nextChar(random);

}

System.out.println(new String(arr));

return new String(arr);

}

}

以上是 【Java】账户初始化密码 的全部内容, 来源链接: www.h5w3.com/114339.html

回到顶部