Bukkit / Java 可自定义的百分比几率
Bukkit / Java customisable percentage chances
各位。我正在尝试在 Bukkit 中制作一个板条箱插件(这并不重要),但我在随机抽奖方面遇到了麻烦。
因为一切都是可定制的(你可以随心所欲地创造无限数量的奖品),我发现很难绘制百分比。
这是我现在的代码:
for (DoubleStack ds : items.values()) {
double chance = ds.getChance();
p.sendMessage(chance + " " + random + " " + ds.getItemStack().getType());
if (random >= chance) {
p.getInventory().addItem(ds.getItemStack());
break;
}
}
DoubleStack 只是我制作的 class 存储物品和获得物品的机会。
如您所见,我使用的方法是错误的,因为机会不现实,而且您有机会获得多个项目(我不想要这个)
那么,我怎样才能做到这一点?
谢谢!
此外,我已经查看了所有其他概率线程,但它们没有涵盖不知道具体概率是多少,它们只是做类似的事情:
int x = new Random().nextInt(100);
if(x <= 10 && x >= 20) {
doSomething(); }
//Repeats with different numbers
试试这个:
for (DoubleStack ds : items.values()) {
int random = new Random().nextInt(100);
int chance = ds.getChance(); // Must be in percentage
if (random < chance) {
p.getInventory().addItem(ds.getItemStack());
break;
}
}
总能获得奖励(机会总和必须为 100):
int random = new Random().nextInt(100);
int counter = 0;
for (DoubleStack ds : items.values()) {
int chance = ds.getChance();
if (random < (chance + counter) && random >= counter) {
p.getInventory().addItem(ds.getItemStack());
break;
}
counter+=chance;
}
各位。我正在尝试在 Bukkit 中制作一个板条箱插件(这并不重要),但我在随机抽奖方面遇到了麻烦。 因为一切都是可定制的(你可以随心所欲地创造无限数量的奖品),我发现很难绘制百分比。 这是我现在的代码:
for (DoubleStack ds : items.values()) {
double chance = ds.getChance();
p.sendMessage(chance + " " + random + " " + ds.getItemStack().getType());
if (random >= chance) {
p.getInventory().addItem(ds.getItemStack());
break;
}
}
DoubleStack 只是我制作的 class 存储物品和获得物品的机会。 如您所见,我使用的方法是错误的,因为机会不现实,而且您有机会获得多个项目(我不想要这个)
那么,我怎样才能做到这一点?
谢谢!
此外,我已经查看了所有其他概率线程,但它们没有涵盖不知道具体概率是多少,它们只是做类似的事情:
int x = new Random().nextInt(100);
if(x <= 10 && x >= 20) {
doSomething(); }
//Repeats with different numbers
试试这个:
for (DoubleStack ds : items.values()) {
int random = new Random().nextInt(100);
int chance = ds.getChance(); // Must be in percentage
if (random < chance) {
p.getInventory().addItem(ds.getItemStack());
break;
}
}
总能获得奖励(机会总和必须为 100):
int random = new Random().nextInt(100);
int counter = 0;
for (DoubleStack ds : items.values()) {
int chance = ds.getChance();
if (random < (chance + counter) && random >= counter) {
p.getInventory().addItem(ds.getItemStack());
break;
}
counter+=chance;
}