从列表中挑选随机物品(Bukkit)

时间:2016-06-24 10:30:48

标签: java bukkit

如何从此列表中选择随机奖励?

Material[] superLucky = { Material.RECORD_10, Material.RECORD_9, Material.RECORD_8  };

            for(Material m : superLucky) {
            if(m == Material.RECORD_10) {
                player.sendMessage(ChatColor.GRAY + "- Enchanted Skull Sword");
            }
            if(m == Material.RECORD_9) {
                player.sendMessage(ChatColor.GRAY + "- Enchanted Skull Helmet");
            }
            if(m == Material.RECORD_8) {
                player.sendMessage(ChatColor.GRAY + "- Enchanted Skull Pickaxe");
            }
        }

3 个答案:

答案 0 :(得分:0)

您会从数组中收到一个随机项:

int idx = new Random().nextInt(superLucky.length);
Material randomItem = superLucky[idx];

答案 1 :(得分:0)

//创建生成随机数的函数

int rrec = (customRandomNumber())%superLucky.length;

使用此索引获取随机奖励。 注意:您可以随机获得相同的奖励。

答案 2 :(得分:0)

有多种方式:

// Math.random() returns a double in the range of 0.0 excluding 1.0
int index = (int)(Math.random() * superLucky.length);

// Random.nextInt() returns an integer in the range of 0.0 excluding the specified number
int index = new Random().nextInt(superLucky.length);

然后做

Material item = superLucky[index];