这是我从头开始制作的密码生成器,但我想添加一个构造函数,以便可以将其用于调用目的,并且还需要创建另一个类来调用它。如果可能的话,我想在这个程序上获得一些帮助。所以我的程序基本上解释了生成随机数符号、大写和小写。我第一次创建密码生成器代码时效果不太好,因为它给了我全部大写或有时全部小写或只是没有其他变量的符号然后我制作了这段代码,告诉程序如果这个变量不存在在显示这些变量之前,它无法创建其他变量。
import java.util.Scanner;
class Passwd {
public Passwd()
{
System.out.println("Hello");
}
public static void main(String[] args) {
new Passwd();
Scanner in;`enter code here`
in = new Scanner(System.in);
int choice = 0;
while (choice != 5) {
System.out.println(" Password Generation Menu ");
System.out.println("********************************************************");
System.out.println("* [1] Lowercase Letters *");
System.out.println("* [2] Lowercase & Uppercase Letters *");
System.out.println("* [3] Lowercase, Uppercase, and Numbers *");
System.out.println("* [4] Lowercase, Uppercase, Numbers, and Symbols *");
System.out.println("* [5] Quit *");
System.out.println("********************************************************");
System.out.println("Enter Selection (1-5): ");
choice = in.nextInt();
if (choice < 1 || choice > 5)
System.out.println("Incorrect option.... ");
else if (choice > 0 && choice < 5) {
System.out.println("Password Length (1-14): ");
int passwordLength = in.nextInt();
char[] password = new char[passwordLength];
int randNum = 0;
if (choice == 1) {
for (int i = 0; i < passwordLength; i++) {
randNum = ((int) (Math.random() * 26 + 97));
while (randNum < 97 || randNum > 122) {
randNum = ((int) (Math.random() * 26 + 97));
}
password[i] = (char) randNum;
}
}
if (choice == 2) {
randNum = ((int) (Math.random() * 26 + 97));
while (randNum < 97 || randNum > 122) {
randNum = ((int) (Math.random() * 26 + 97));
}
password[0] = (char) randNum;
randNum = ((int) (Math.random() * 26 + 65));
while (randNum < 65 || randNum > 90) {
randNum = ((int) (Math.random() * 26 + 65));
}
password[1] = (char) randNum;
for (int i = 2; i < passwordLength; i++) {
randNum = ((int) (Math.random() * 122 - 65 + 1)) + 65;
while ((randNum < 65 || randNum > 90) && (randNum < 97 || randNum > 122)) {
randNum = ((int) (Math.random() * 122 - 65 + 1)) + 65;
}
password[i] = (char) randNum;
}
}
if (choice == 3) {
randNum = ((int) (Math.random() * 26 + 97));
while (randNum < 97 || randNum > 122) {
randNum = ((int) (Math.random() * 26 + 97));
}
password[0] = (char) randNum;
randNum = ((int) (Math.random() * 26 + 65));
while (randNum < 65 || randNum > 90) {
randNum = ((int) (Math.random() * 26 + 65));
}
password[1] = (char) randNum;
randNum = ((int) (Math.random() * 26 + 48));
while (randNum < 48 || randNum > 57) {
randNum = ((int) (Math.random() * 26 + 48));
}
password[2] = (char) randNum;
for (int i = 3; i < passwordLength; i++) {
randNum = ((int) (Math.random() * 122 - 48 + 1)) + 48;
while ((randNum < 65 || randNum > 90) && (randNum < 97 || randNum > 122) && (randNum < 48 || randNum > 57)) {
randNum = ((int) (Math.random() * 122 - 48 + 1)) + 48;
}
password[i] = (char) randNum;
}
}
if (choice == 4) {
randNum = ((int) (Math.random() * 26 + 97));
while (randNum < 97 || randNum > 122) {
randNum = ((int) (Math.random() * 26 + 97));
}
password[0] = (char) randNum;
randNum = ((int) (Math.random() * 26 + 65));
while (randNum < 65 || randNum > 90) {
randNum = ((int) (Math.random() * 26 + 65));
}
password[1] = (char) randNum;
randNum = ((int) (Math.random() * 10 + 48));
while (randNum < 48 || randNum > 57) {
randNum = ((int) (Math.random() * 10 + 48));
}
password[2] = (char) randNum;
randNum = ((int) (Math.random() * 7 + 33));
while (randNum < 33 || randNum > 39) {
randNum = ((int) (Math.random() * 7 + 33));
}
password[3] = (char) randNum;
for (int i = 4; i < passwordLength; i++) {
randNum = ((int) (Math.random() * 122 - 33 + 1)) + 33;
while ((randNum < 65 || randNum > 90) && (randNum < 97 || randNum > 122) && (randNum < 48 || randNum > 57) && (randNum < 33 || randNum > 39)) {
randNum = ((int) (Math.random() * 122 - 33 + 1)) + 33;
}
password[i] = (char) randNum;
}
}
for (int j = 0; j < passwordLength; j++)
System.out.print(password[j]);
System.out.println("\n");
}
}
System.out.println("Thank You for using World's Best Password generator \n");
}
}