한 가지를 제외하고는 모든 것이 작동한다고 생각합니다. 메소드를 main에서 두 번 이상 호출하면 동일한 비밀번호를 계속 작성합니다. 여기 암호를 만드는 자바 프로그램을 만들려고합니다.
는 암호 생성을위한 클래스입니다 :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;
}
}
이 내가 주에있는 것입니다 :
Password: seggdpsptkxqo
Password: seggdpsptkxqo
Password: seggdpsptkxqo
Password: seggdpsptkxqo
:
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());
}
}
나는이 같은 출력을 얻을 것이다 다른 실수 나 나쁜 습관을 지적 해 주셔도됩니다.
이 아니라 "만드는"방법, 생성자에 암호를 계산하는 다음 RandomStringUtils에서
특정 길이의 임의의 문자열을 생성하는 방법이있다 ;) – Ray
실제 생성 코드를 생성자에서 자신의 메서드 또는 createPassword 메서드로 옮깁니다. – LaneL
나는 근본적인 오해가 있다고 생각합니다. 다음을보십시오 : https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html – smddzcy