2016-12-30 1 views
1

한 가지를 제외하고는 모든 것이 작동한다고 생각합니다. 메소드를 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()); 
    } 
} 

나는이 같은 출력을 얻을 것이다 다른 실수 나 나쁜 습관을 지적 해 주셔도됩니다.

+3

이 아니라 "만드는"방법, 생성자에 암호를 계산하는 다음 RandomStringUtils에서

특정 길이의 임의의 문자열을 생성하는 방법이있다 ;) – Ray

+0

실제 생성 코드를 생성자에서 자신의 메서드 또는 createPassword 메서드로 옮깁니다. – LaneL

+0

나는 근본적인 오해가 있다고 생각합니다. 다음을보십시오 : https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html – smddzcy

답변

4

코드를 살펴 보겠습니다. 를 변경하지 않고,

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; 
    } 
} 

그런 다음 createPassword 방법, 당신은 당신이 생성자에서 생성 된 해당 암호를 반환 : 당신의 생성자에서

, 당신은 당신의 길이를 초기화 한 다음 비밀번호를 생성

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

따라서 createPassword으로 전화 할 때마다 똑같은 결과가 나옵니다. 당신은 사용할 수

Password: mvlqqgfmotldc 
Password: inneuyuynqakd 
Password: hstlfsfspfaua 
Password: jgngsmdiguxcy 
+1

매우 감사합니다. 왜 내가 암호 = ""를 가져야하는지 말해 줄 수 있습니까? 암호를 만드시겠습니까? – Budaika

+1

@ user3261806 당신은 그 라인을 제거함으로써 자신을 발견 할 수 있습니다. – Tom

+0

@ user3261806 - 예. 함께 생각해 봅시다. 우리가 완전히 제거한다고 상상해보십시오. (직접 시도해보십시오.) 왜 this.password.length()를 호출하려고 할 때'NullPointerException'을 얻을 것입니다. 이제 생성자로 다시 이동한다고 가정 해 봅시다. 이제 우리는'createPassword()'를 호출 할 때마다 같은 패스워드를 다시 얻는다. 왜 이것이 이럴 수 있다고 생각하니? 비밀번호를 어떻게 생성하는지보십시오. 'while (this.password.length()

2

새 PasswordRandomizer 프로그램을 만들어야합니다. 그렇지 않으면 매번 암호를 만들지 않습니다. 예를 들어 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()); 
    } 
} 
+0

이것은 이름이' createPassword'를 단지'getPassword() '로 변경하십시오. – weston

0

: 출력처럼 당신이 얻을, 우리는 당신의 Program을 실행할 때, 지금

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; 
    } 

} 

:의 우리가 단지 createPassword 방법으로 아래의 암호를 생성하는 코드를 이동하면 어떻게되는지 보자 Apache Commons Lang 라이브러리. randomAlphabetic (INT 수)

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

관련 문제