随机字符生成器

时间:2016-10-01 23:15:44

标签: java arrays arraylist random

按下按钮时如何显示此随机值?

//random gender         
    List<String> Genderlist = new ArrayList<>();
        Genderlist.add("Gender: Male");
        Genderlist.add("Gender: Female");
            String randomGender = Genderlist.get(new Random().nextInt(Genderlist.size()));
                System.out.println(" Your "+ randomGender); 

         // random class       
    List<String> classlist = new ArrayList<>(); 
            classlist.add("Class: Fighter");
            classlist.add("Class: Rogue");
            classlist.add("Class: Cleric");
            classlist.add("Class: Mage");
            classlist.add("Class: Druid");
            classlist.add("Class: Monk");
            classlist.add("Class: Paladin");
            classlist.add("Class: Ranger");
            classlist.add("Class: Sorcerer");
            classlist.add("Class: Warlock");
            classlist.add("Class: Wizard");
            classlist.add("Class: Barbarian");
            classlist.add("Class: Bard");
                String randomClass = classlist.get(new Random().nextInt(classlist.size()));
                System.out.println(" Your "+ randomClass); 

每当我运行它,我就可以正常工作,但现在我想要一个运行它的随机生成器按钮。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

enum Stat {
    STR,
    DEX,
    CON,
    INT,
    WIS,
    CHA
}

class Character {
    final private Set<Stat> stats;

    public Character() {
        stats = EnumSet.noneOf(Stat.class);

        Random random = new Random();
        final int statCount = 3;
        while(stats.size() < statCount) { {
            stats.add(Stat.values()[random.nextInt(Stat.values().length)]);
        }
    }
}