[英]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.