密码生成器
1.程序实现逻辑
- 获取输入(密码长度,特殊字符个数,数字个数)
- 随机产生字母,产生数字,产生特殊字符
- 拼凑成字符串
- 打乱字符串,输出
2.java程序
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class CodeBuilder {
private static int len;//密码长度
private static int specialLen;//特殊字符长度
private static int numLen;//数字长度
private static String numStr;//数字字符
private static char[] strArray;//密码字符数组
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
System.out.print("What's the minimum length?");
do {
len = scanner.nextInt();
break;
}while(true);
System.out.print("How many special characters?");
do {
specialLen = scanner.nextInt();
break;
}while(true);
System.out.print("How many numbers?");
do {
numLen = scanner.nextInt();
break;
}while(true);
scanner.close();
int[] specialChar = {33,48,58,65,91,96,122,127};
int[] lowerCase = {97,123};
int d;
char e;
StringBuilder str = new StringBuilder();
//添加字母
for (int i = 0;i < len - specialLen - numLen;i++){
d = (int) (Math.random()*(lowerCase[1] - lowerCase[0]) + lowerCase[0]);
e = (char) d;
str.append(e);
}
//添加数字
for (int i = 0;i < numLen;i++){
d = (int) (Math.random()*10);
numStr = String.valueOf(d);
str.append(numStr);
}
//添加特殊符号
for (int i = 0;i < specialLen;i++){
d = (int) (Math.random()*3)*2;
d = (int) (Math.random()*(specialChar[d+1] - specialChar[d]) + specialChar[d]);
e = (char) d;
str.append(e);
}
//将密码打乱
strArray = str.toString().toCharArray();
List<String> lst = new ArrayList<String>();
for(int i=0;i<strArray.length;i++){
lst.add((String.valueOf(strArray[i])));
}
Collections.shuffle(lst);
//打印密码
System.out.println("Your password is ");
for (int i = 0;i < str.length();i++){
System.out.print(lst.get(i));
}
}
}