按随机顺序排列

时间:2016-01-03 18:53:22

标签: java

所以对于学校我必须制作一个密码生成器,我们可以在其中设置密码的数量以及小写字母,大写字母,符号和数字的数量。这是代码:

package vantroys;

import java.util.Random;

public class PasswordGeneratorTest {

/**
 * @param args
 */
public static void main(String args[]) {

    int lowerCase;
    int randomNum;
    Random rand = new Random();



    SimpleInOutDialog num = new SimpleInOutDialog("randomgenerator");
    PasswordGeneratorClass test = new PasswordGeneratorClass();


    test.setAmountPasswords(num.readInteger("How many passwords do you want to generate?"));









    while (test.getAmountPasswords()>0){
        test.setAmountLetters(num.readInteger("How many letters should this password contain?"));
        test.setAmountUpperCase(num.readInteger("How many of these letters should be upper case?"));

        if (test.getAmountUpperCase()>test.getAmountLetters()){
            num.showString("", "This isn't possible, there aren't enough letters.");
            num.stop();
        }

        test.setAmountNumbers(num.readInteger("How many numbers should the password contain?"));
        test.setAmountSymbols(num.readInteger("How many symbols should the password contain?"));

        lowercase= test.getAmountLetters()-test.getAmountUpperCase();

        while (lowercase>0){
            char a = (char) (rand.nextInt(26) + 'a');
            lowercase=lowercase-1;

        }

        while (test.getAmountUpperCase()>0){
            char b = (char) (rand.nextInt(26) + 'A');
            test.setAmountUpperCase(test.getAmountUpperCase()-1);
        }

        while (test.getAmountNumbers()>0){
            randomNum = rand.nextInt((10 - 1) + 1) + 1;
            System.out.println(randomNum);
            test.setAmountNumbers(test.getAmountNumbers()-1);
        }

        while (test.getAmountSymbols()>0){
            char c = (char) (rand.nextInt(0xB4 - 21 + 1) + 21);
            System.out.println(c);
            test.setAmountSymbols(test.getAmountSymbols()-1);
        }
        test.setAmountPasswords(test.getAmountPasswords()-1);
    }
}

一切正常但现在我应该把我生成的所有字母,数字和符号按随机顺序排列,我无法弄清楚如何去做,有没有简单的方法可做我只是没有看到这个?

1 个答案:

答案 0 :(得分:0)

将所有字符放入列表中。然后使用Collections.shuffle()

相关问题