Java switch statements outputting the same numbers

大城市里の小女人 提交于 2019-12-25 01:05:09

问题


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.

Example screenshot of all the sword damages as 4: http://puu.sh/hevSP/7974257751.png

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.

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;
    }

回答1:


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);
    }
}


来源:https://stackoverflow.com/questions/29641167/java-switch-statements-outputting-the-same-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!