这是我从头开始制作的密码生成器,但我想添加一个构造函数,以便我可以将其用于调用目的,还需要创建另一个类来调用它。如果可能的话,我想在此程序上获得一些帮助。因此,我的程序基本上解释了生成随机数符号,大小情况和小写字母。我第一次创建密码生成器代码时的工作不佳,因为它给了我所有大写,有时甚至没有其他变量的符号然后,我制作了此代码,我告诉程序,如果此变量不存在,则在显示这些变量之前将无法创建其他变量。
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");
}
}
您可以做的是应用OO原理,并创建一个负责生成密码的类,例如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);
// ...
}
}