问题描述:

i have this code;

@SuppressWarnings("deprecation")

@EventHandler

public void entityShoot(EntityDeathEvent e) {

ArrayList<ItemStack> drops = new ArrayList<ItemStack>();

Random rand = new Random();

int sword = rand.getInt(1) + 1;

int chest = rand.getInt(9) + 1;

if(sword == 1)

{

drops.add(new ItemStack(swordID));

}

if(chest == 1)

{

drops.add(new ItemStack(chestID));

}

}

But there is a error in getInt.

If i change it to nextInt, there is no random..

Its for Minecraft Bukkit.

Can you correct the code?

I don´t understand..

Thank you!

Regards, René :)

网友答案:

this

 int sword = rand.getInt(1) + 1;
 int chest = rand.getInt(9) + 1;

shall be

 int sword = rand.nextInt(1) + 1;
 int chest = rand.nextInt(9) + 1;

instead

Good Luck

网友答案:

getInt() is not a method, that will not even compile.

nextInt()'s parameter is exclusive so a call to nextInt(1) will always give you a zero. That might be why it appears not to be random to you.

Also, instead of adding a 1 to chest and then checking to see if equals 1, just don't add anything and check for it to equal zero. Same for sword.

网友答案:

should be nextInt()

int sword = rand.nextInt(1) + 1;
int chest = rand.nextInt(9) + 1;

Random has no getInt. See Random javadoc

网友答案:

you can also use Math.Random(), it will give you a number between 0 and 0.9999....

相关阅读:
Top