我正在尝试使用ASCII值创建密码生成器,其中包含小写字母,小写和大写,数字等菜单选项。
我得到的程序只是小写,但我不知道如何分组,所以随机数选择多个ASCII组。我们不能在这个类中使用数组,因为我们尚未涵盖那些数组。
import java.util.Scanner;
import java.util.Random;
import java.util.Scanner;
import java.io.IOException;
public class Password {
public static void main(String [] args) throws IOException
{
Scanner in = new Scanner(System.in);
System.out.println(" Password Generation Menu");
System.out.println("====================================================");
System.out.println("[1] Lowercase Letters");
System.out.println("[2] Lowercase & Uppercase");
System.out.println("[3] Lowercase, Uppercase, and Numbers");
System.out.println("[4] Lowercase, Uppercase, Numbers, and Punctuation");
System.out.println("[5] Quit");
System.out.println("====================================================");
System.out.println();
System.out.println("Enter Selection: ");
int choice = in.nextInt();
System.out.println("Enter Length of Password: ");
int n = in.nextInt();
for(int i = 0; i < n; i++){
if(choice == 1){
Random r = new Random();
char c = (char)(r.nextInt(26) + 'a');
System.out.print(c);
}
else if(choice == 2){
Random r = new Random();
char c = (char)(r.nextInt(26) + 'A');
System.out.print(c);
}
else if(choice == 3){
Random r = new Random();
char c = (char)(r.nextInt(10) + '0');
System.out.print(c);
}
else if(choice == 4){
Random r = new Random();
char c = (char)(r.nextInt(26) + 'a');
System.out.println(c);
}
else{
System.exit(0);
}
}
}
}
答案 0 :(得分:0)
您可以先创建一个随机数,决定下一个字母取自哪个类,然后生成另一个随机数,以选择该特定组的实际字母。