首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >输出相同数字的Java switch语句

问输出相同数字的Java switch语句
EN

Stack Overflow用户
提问于 2015-04-15 11:36:33
回答 1查看 109关注 0票数 0

我的随机产卵代码总是在我每次运行代码时将武器伤害设置为相同的数字。虽然有时它会在3(对于短剑),4,6和8之间变化,但地图上每个物品的伤害都是相同的。我想这可能是因为当我在每个案例结束时都有回报时,没有中断,但这不是‘案例’,仍然只输出同样的几个伤害。

所有剑伤害的示例屏幕截图为4:http://puu.sh/hevSP/7974257751.png

这不是硬编码的伤害数字,因为我给每个数字加上了'random.nextInt(5)‘,它们仍然是相同的数字。

结尾处的注释剑代码设置了我放入的任何东西的损害,所以我知道它是导致问题的switch语句之一。谢谢。

代码语言:javascript
复制
    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;
    }
EN

回答 1

Stack Overflow用户

发布于 2015-04-15 13:26:05

您必须定义此类并使用其随机函数,而不是调用random.nextInt

代码语言:javascript
复制
public class Numbers {
    Random randnum;

    public Numbers() {
        randnum = new Random();
        randnum.setSeed(123456789);
    }

    public int random(int i){
        return randnum.nextInt(i);
    }
}
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29641167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档