有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java正在尝试创建密码生成器

我正在尝试使用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);
        }
}


}
}

共 (1) 个答案

  1. # 1 楼答案

    您可以首先创建一个随机数来决定下一个字母是从哪个类中提取的,然后生成另一个随机数来从该特定组中选择实际的字母