Question

I am writing a small program.
I am a beginner at programming java.
Here is the code:
package dnd;

public class Monster {
    static String name;
    static int level;
    static String rang;
    static String Class;

    static double[] Strength = {1, 1, 0};
    static double[] Endurance = {1, 1, 0};
    static double[] Agility = {1, 1, 0};
    static double[] Precision = {1, 1, 0};
    static double[] Wisdom = {0, 1, 1};
    static double[] Intelegence = {0, 1, 1};

    public static void Monster(String nameC, int levelC, String classesC, int rangC){

        name = nameC;

        level = levelC;

        switch(rangC){
            case 1:
                rang = "Civillian";
            case 2:
                rang = "Soldier";
            case 3:
                rang = "Veteran";
            case 4:
                rang = "Commander";
            case 5:
                rang = "Boss";
            case 6:
                rang = "Elite";
            default:
                rang = "Civillian";
        }

        Class = classesC;

        switch(Class){
            case "Warrior":
                Strength[1] = 2;
                Endurance[1] = 2;
                Intelegence[1] = 0.5;
            case "Archer":
                Agility[1] = 2;
                Precision[1] = 2;
                Endurance[1] = 0.5;
            case "Rouge":
                Agility[1] = 2;
                Intelegence[1] = 2;
                Endurance[1] = 0.5;
            case "Mage":
                Wisdom[1] = 2;
                Intelegence[1] = 2;
                Strength[1] = 0.5;
            case "Priest":
                Wisdom[1] = 2;
                Strength[1] = 0.5;
            case "Druid":
                Intelegence[1] = 2;
                Agility[1] = 0.5;
        }
    }

    public static void defaultStaringPoints(){

        int StP;
        int EnP;
        int InP;
        int AgP;
        int PrP;
        int WiP;

        switch(Class){


            case "Warrior":
                //Strength
                //Endurance
                //-Intelegence

                StP = (int) (Math.random() * 2);
                EnP = (int) (Math.random() * (3-StP));
                InP = (int) (Math.random() * (3-(StP+EnP)));

                Strength[0] = Strength[0] + StP;
                Endurance[0] = Endurance[0] + EnP;
                Intelegence[0] = Intelegence[0] + InP;

            case "Archer":
                //Agility
                //Precision
                //-Endurance

                AgP = (int) (Math.random() * 2);
                PrP = (int) (Math.random() * (3-AgP));
                EnP = (int) (Math.random() * (3-(AgP+PrP)));

                Agility[0] = Agility[0] + AgP;
                Precision[0] = Precision[0] + PrP;
                Endurance[0] = Endurance[0] + EnP;

            case "Rouge":
                //Agility
                //Intelegence
                //-Endurance

                AgP = (int) (Math.random() * 2);
                InP = (int) (Math.random() * (3-AgP));
                EnP = (int) (Math.random() * (3-(AgP+InP)));

                Agility[0] = Agility[0] + AgP;
                Intelegence[0] = Intelegence[0] + InP;
                Endurance[0] = Endurance[0] + EnP;

            case "Mage":
                //Wisdom
                //Intelegence
                //-Strength

                WiP = (int) (Math.random() * 2);
                InP = (int) (Math.random() * (3-WiP));
                StP = (int) (Math.random() * (3-(WiP+InP)));

                Wisdom[0] = Wisdom[0] + WiP;
                Intelegence[0] = Intelegence[0] + InP;
                Strength[0] = Strength[0] + StP;

            case "Priest":
                //Wisdom
                //-Strength

                WiP = (int) (Math.random() * 3);
                StP = (int) (Math.random() * (3-WiP));

                Wisdom[0] = Wisdom[0] + WiP;
                Strength[0] = Strength[0] + StP;

            case "Druid":
                //Intelegence
                //-Agility

                InP = (int) (Math.random() * 3);
                AgP = (int) (Math.random() * (3-InP));

                Intelegence[0] = Intelegence[0] + InP;
                Agility[0] = Agility[0] + AgP;

        }    
    }   
}

Then I create a new Monster,
run Goblin.Monster("Goblin", 1, "Rouge", 2)
and Goblin.defaultStaringPoints()
the output for Arrays.toString(Goblin.Wisdom) should be [0, 1, 1],
but instead it is [1, 2, 1], [2, 2, 1] or even [3, 2, 1]. I feel, that I am just overseeing something,
but I checked over 10 times.
Thank You in advance!

Was it helpful?

Solution

In each of your switch statements, you need a break; statement at the end of each case. For example:

    switch(rangC){
    case 1:
        rang = "Civillian";
        break;
    case 2:
        rang = "Soldier";
        break;
    ...
}

Without the break;, execution simply falls through to the next case.

Also, you don't have a case for "Goblin" in any of your switch statements.

OTHER TIPS

When you call defaultStartingPoints(), it randomizes the stats so that the Wisdom stat is no longer [0, 1, 1].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top