密码随机客户:创建相同的密码

时间:2015-11-24 17:28:26

标签: java random

重点是制作密码随机用户,但我的问题是我的代码始终使用相同的密码。我尝试了各种各样的东西,这是我通过互联网获得的最新产品。

主要课程:

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

Program类:

import java.util.Random;
public class PasswordRandomizer {
    // Define the variables

    private Random password = new Random();
    private int length;
    private String character; 

    public PasswordRandomizer(int length) {
        //creates a new object, uses the given password length
        this.length = length;

        String characters = "";

            Random rndNumbers = new Random();
            int randomnumber = 0;


            for (int nbr = 1; nbr < length; nbr++) {
                randomnumber = rndNumbers.nextInt(25);

                char character = "abcdefghijklmnopqrstuvwxyz".charAt(randomnumber);
                characters = characters + character; 
            }

        System.out.println(characters);
        this.character = characters;
    }


    public String createPassword() {
        // write code that returns a randomized password
        PasswordRandomizer randomizer = new PasswordRandomizer(13);
        //consists of symbols a-z and is of the length given as a parameter to the constructor
        return this.character;  
    }
}

PasswordRandomizer方法中,行System.out.println(characters);每次都会打印出随机密码。但是createPassword方法中没有使用不同的密码,只有第一个密码在输出中始终产生相同的密码。

3 个答案:

答案 0 :(得分:2)

正如我在评论中提到的,您的方法createPassword()会调用构造函数并创建一组丢失的新实例变量。将“随机化”代码移动到此方法可以修复它。查看sample at Ideone。修改后的构造函数和createPassword()代码如下所示。

public PasswordRandomizer (int length) {
        //creates a new object, uses the given password length
        this.length = length;

}     

public String createPassword() {
        String characters = "";

        Random rndNumbers = new Random();
        int randomnumber = 0;
        for (int nbr = 1; nbr < length; nbr++) {
            randomnumber = rndNumbers.nextInt(25);
            char character = "abcdefghijklmnopqrstuvwxyz".charAt(randomnumber);
            characters = characters + character; 
        }

        System.out.println(characters);
        return characters;  
}

答案 1 :(得分:0)

在您的示例中,您创建了2个Random个实例和2个PasswordRandomizer个实例。

你继续调用createPassword()(在其中一个实例上)创建一个PasswordRandomizer对象(然后抛弃它),然后只返回this.character;(不是randomizer.character会给你你寻求的行为。

答案 2 :(得分:0)

public String createPassword() {
        // write code that returns a randomized password
        PasswordRandomizer randomizer = new PasswordRandomizer(13);
        //consists of symbols a-z and is of the length given as a parameter to the constructor
        return randomizer.character;  //This could be a simple fix.
    }