输出相同数字的 Java switch 语句 [英] Java switch statements outputting the same numbers
问题描述
每次我运行代码时,我的对象随机生成代码总是将武器伤害设置为相同的数字.虽然它有时会在 3(匕首)、4、6 和 8 之间变化,但地图上每个物品的伤害都是一样的.我认为这可能是因为在每个案例结束时我都没有退货,但这不是案例",仍然只输出相同的少量损坏.
My object randomised spawn code always sets a weapons damage to the same number every time I run the code. Although it sometimes changes between 3 (for daggers), 4, 6 and 8, but every item's damage on the map is the same. I thought it might be because there were no breaks back when I had returns at the end of each case however this wasn't the 'case' and still only outputs the same few damages.
所有剑伤害的示例截图为 4:http://puu.sh/hevSP/7974257751.png
Example screenshot of all the sword damages as 4: http://puu.sh/hevSP/7974257751.png
这不是硬编码的伤害数字,因为我在每个数字中都添加了random.nextInt(5)",它们仍然是相同的数字.
It's not the hard coded damage numbers because I added 'random.nextInt(5)' to each of them and they were all still the same number.
末尾的注释剑代码对我输入的任何内容造成了损害,所以我知道这是导致问题的 switch 语句之一.谢谢.
The commented sword code at the end sets the damage to whatever I put in, so I know it's one of the switch statements causing the problem. Thanks.
public static Object itemRoulette(String quality, int X, int Y) {
Object returnItem = null;
//Rolls a random item based on the input quality between 1-3.
switch(quality)
{
//First tier of quality.
case "1":
{
switch (random.nextInt(5) + 1)
{
case 1: {Dagger dagger = new Dagger(); World[X][Y].treasureName = "dagger"; dagger.setDamage(2); World[X][Y].treasureName = "dagger"; returnItem = dagger; break;}
case 2: {Potion potion = new Potion(); World[X][Y].treasureName = "potion"; potion.setType("health"); returnItem = potion; break;}
case 3: {Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(4); returnItem = sword; break;}
case 4: {Potion potion = new Potion(); World[X][Y].treasureName = "potion"; potion.setType("health"); returnItem = potion; break;}
case 5: {Dagger dagger = new Dagger(); World[X][Y].treasureName = "dagger"; dagger.setDamage(3); returnItem = dagger; break;}
}
break;
}
//Second tier of quality
case "2":
{
switch (random.nextInt(5) + 1)
{
case 1: {Dagger dagger = new Dagger(); World[X][Y].treasureName = "dagger"; dagger.setDamage(4); returnItem = dagger; break;}
case 2: {Potion potion = new Potion(); World[X][Y].treasureName = "potion"; potion.setType("health"); returnItem = potion; break;}
case 3: {Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(5); sword.setDefence(1); returnItem = sword; break;}
case 4: {Potion potion = new Potion(); World[X][Y].treasureName = "potion"; potion.setType("health"); returnItem = potion; break;}
case 5: {Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(6); sword.setDefence(1); returnItem = sword; break;}
}
break;
}
//Third tier of quality.
case "3":
{
switch (random.nextInt(5) + 1)
{
case 1: {Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(6); sword.setDefence(1); returnItem = sword; break;}
case 2: {Potion potion = new Potion(); World[X][Y].treasureName = "potion"; potion.setType("health"); returnItem = potion; break;}
case 3: {Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(7); sword.setDefence(1); returnItem = sword; break;}
case 4: {Potion potion = new Potion(); World[X][Y].treasureName = "potion"; potion.setType("health"); returnItem = potion; break;}
case 5: {Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(8); sword.setDefence(2); returnItem = sword; break;}
}
break;
}
case "dagger":
{
World[X][Y].treasureName = "dagger";
Dagger dagger = new Dagger();
dagger.setDamage(3);
dagger.setDefence(0);
returnItem = dagger;
break;
}
case "sword":
{
World[X][Y].treasureName = "sword";
Sword sword = new Sword();
sword.setDamage(6);
sword.setDefence(1);
returnItem = sword;
break;
}
}
//Sword sword = new Sword(); World[X][Y].treasureName = "sword"; sword.setDamage(40); returnItem = sword;
return returnItem;
}
推荐答案
你必须定义这个类并使用它的随机函数而不是调用random.nextInt
you must define this class and use its random function instead of calling random.nextInt
public class Numbers {
Random randnum;
public Numbers() {
randnum = new Random();
randnum.setSeed(123456789);
}
public int random(int i){
return randnum.nextInt(i);
}
}
这篇关于输出相同数字的 Java switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!