简体   繁体   English

我正在尝试制作一个可以创建密码的Java程序

[英]I'm trying to make a java program that creates passwords

I think I have everything working except for one thing. 我想除了一件事情,我一切都可以工作。 When I call the method more than once on main it keeps creating the same password. 当我在主方法上多次调用该方法时,它会继续创建相同的密码。

Here's the class for the password creation: 这是密码创建的类:

import java.util.Random;

public class PasswordRandomizer {
    // Define the variables
    private int length;
    private String password;
    private Random random = new Random();
    private char symbol;

    public PasswordRandomizer(int length) {
        // Initialize the variable
        password = "";
        this.length = length;
        while (this.password.length() < this.length) {
            this.symbol = "abcdefghijklmnopqrstuvwxyz".charAt(this.random.nextInt(25));
            this.password += symbol;
        }
    }

    public String createPassword() {
        // write code that returns a randomized password
        return this.password;
    }
}

and this is what I have in main: 这就是我的主要内容:

public class Program {
    public static void main(String[] args) {
        PasswordRandomizer randomizer = new PasswordRandomizer(13);
        System.out.println("Password: " + randomizer.createPassword());
        System.out.println("Password: " + randomizer.createPassword());
        System.out.println("Password: " + randomizer.createPassword());
        System.out.println("Password: " + randomizer.createPassword());
    }
}

I would get an output like this: 我会得到这样的输出:

Password: seggdpsptkxqo
Password: seggdpsptkxqo
Password: seggdpsptkxqo
Password: seggdpsptkxqo

Feel free to point out any other mistakes or bad habits I have, I'm still pretty new at this. 随时指出我有任何其他错误或不良习惯,对此我还是很新的。

Let's look at your code. 让我们看看您的代码。

In your constructor, you initialize your length, and then you generate your password: 在构造函数中,初始化长度,然后生成密码:

public PasswordRandomizer(int length) {
    // Initialize the variable
    password = "";
    this.length = length;
    while (this.password.length() < this.length) {
        this.symbol = "abcdefghijklmnopqrstuvwxyz".charAt(this.random.nextInt(25));
        this.password += symbol;
    }
}

Then, in your createPassword method, you just return that password you generated in the constructor, without changing it: 然后,在您的createPassword方法中,只需返回在构造函数中生成的密码,而不更改它:

public String createPassword() {
    // write code that returns a randomized password
    return this.password;
}

And so, every time you call createPassword , you are going to get the same thing. 因此,每次调用createPassword ,都会得到相同的结果。 Let's see what happens if we just move that code which generates the password down into the createPassword method: 让我们看看如果仅将生成密码的代码移到createPassword方法中会发生什么:

import java.util.Random;

public class PasswordRandomizer {
    // Define the variables
    private int length;
    private String password;
    private Random random = new Random();
    private char symbol;

    public PasswordRandomizer(int length) {
        // Initialize the variable
        this.length = length;
    }

    public String createPassword() {
        // write code that returns a randomized password
        password = "";
        while (this.password.length() < this.length) {
            this.symbol = "abcdefghijklmnopqrstuvwxyz".charAt(this.random.nextInt(26));
            this.password += symbol;
        }
        return this.password;
    }

}

Now, when we run your Program , you get output like: 现在,当我们运行您的Program ,您将获得如下输出:

Password: mvlqqgfmotldc
Password: inneuyuynqakd
Password: hstlfsfspfaua
Password: jgngsmdiguxcy

You should create a new PasswordRandomizer program, otherwise, you will not create a password each time. 您应该创建一个新的PasswordRandomizer程序,否则,您将不会每次都创建一个密码。 For instance, if you don't rewrite the PasswordRandomizer class, you can do : 例如,如果您不重写PasswordRandomizer类,则可以执行以下操作:

package test;
public class Program {
    public static void main(String[] args) {
        System.out.println("Password: " + new PasswordRandomizer(13).createPassword());
        System.out.println("Password: " + new PasswordRandomizer(13).createPassword());
        System.out.println("Password: " + new PasswordRandomizer(13).createPassword());
        System.out.println("Password: " + new PasswordRandomizer(13).createPassword());
    }
}

You could use the Apache Commons Lang library. 您可以使用Apache Commons Lang库。

In the RandomStringUtils there is a method to generate a random String with a certain length: randomAlphabetic(int count) 在RandomStringUtils中,有一种生成具有一定长度的随机String的方法:randomAlphabetic(int count)

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/RandomStringUtils.html#randomAlphabetic-int- http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/RandomStringUtils.html#randomAlphabetic-int-

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java-我试图做一个计时器 - Java - I'm trying to make a timer 我正在尝试使用GUI在Java中制作计算器 - I'm Trying To Make A Calculator in Java with a GUI 我正在尝试制作一个java数组代码 - I'm trying to make a java array code 我正在尝试制作 Java 文件服务器 - I'm trying to make a Java File Server 在这个程序(Java)中,我试图做一个骰子滚轴。 我如何使它滚动很多次并添加滚动? - In this program(Java) I'm trying to make a dice roller. How do I make it so it rolls a bunch of times and adds the rolls? 我正在尝试在二进制转换器程序中使用特定键&#39;,但它会产生语法错误 - I'm trying to use the specific key ' in my binary converter program, but it creates a syntax error 我正在尝试与 2 个赛车手一起制作一个程序,但它一直给我一个 IllegalThreadStateException - I'm trying to make a program with 2 racers and it keeps giving me an IllegalThreadStateException 我正在尝试编写一个递归Java程序来创建Serpinski三角形 - I'm trying to write a recursive java program to create the Serpinski triangle 尝试运行创建Java数据库的程序时出错。 我错过了什么? - Error when trying to run a program that creates a Java DataBase. What am I missing? 我正在尝试制作一个简单的程序,用“*”符号替换&#39;a&#39;,&#39;e&#39;,&#39;i&#39;,但我收到此错误代码 - I am trying make a simple program that replaces 'a', 'e', 'i' with the “*” symbol but i'm getting this error code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM