有人可以帮我解决我写的代码吗? (JAVA)

时间:2014-04-18 15:55:22

标签: java constructor switch-statement

我正在写一个小程序 我是java编程的初学者 这是代码:
    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;

        }    
    }   
}

然后我创造了一个新的怪物,
运行Goblin.Monster("Goblin", 1, "Rouge", 2)
Goblin.defaultStaringPoints()
Arrays.toString(Goblin.Wisdom)的输出应为[0,1,1],
但相反,它是[1,2,1],[2,2,1]或甚至[3,2,1]。 我觉得,我只是在监督一些事情,
但我检查了10次 提前谢谢!

2 个答案:

答案 0 :(得分:2)

在每个switch语句中,您需要在每个break;的末尾添加case语句。例如:

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

如果没有break;,执行只会落到下一个案例中。

此外,在case"Goblin"语句中,您switch {{1}}还没有。{/ p>

答案 1 :(得分:-1)

当你致电defaultStartingPoints()时,它会随机化统计数据,以便智慧统计数据不再是[0, 1, 1]