这是我从头开始制作的密码生成器,但是我想添加一个构造函数,以便可以将其用于调用目的,并且还需要创建另一个类来调用它。如果可能的话,我想在这个程序上获得一些帮助。 因此,我的程序基本上解释了如何生成随机数符号,大写和小写。第一次创建密码生成器代码时,它不能很好地工作,因为它给了我所有的大写字母,有时是全部的小写字母,或者只是没有其他变量的符号 然后,我在告诉程序的地方编写了这段代码,如果该变量不存在,则无法显示其他变量,直到显示这些变量为止。
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");
}
}
答案 0 :(得分:0)
您可以做的是应用面向对象的原理,并创建一个负责生成密码的类,例如PasswordGenerator
。看起来像:
public class PasswordGenerator {
public String generatePassword(int passwordType) {
// All your password generation logic
}
}
因此您可以在其他课程中使用它。
public class PasswordThing {
public void doSomethingWithPassword() {
PasswordGenerator passwordGenerator = new PasswordGenerator();
String password = password.generatePassword(1);
// ...
}
// or
public static void main(String[] args) {
// Get user choice from command line as you did in your sample
PasswordGenerator passwordGenerator = new PasswordGenerator();
String password = passwordGenerator.generatePassword(userChoice);
// ...
}
}