繁体   English   中英

有人可以帮助我编写我正在编写的代码吗? (爪哇)

[英]Can someone please help me with the code I am writing? (Java)

我正在写一个小程序。
我是java编程的初学者。
这是代码:
包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 多次。
先感谢您!

在你的每一个switch语句中,你都需要break; 每个case结束时的声明。 例如:

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

没有break; ,执行简单地落入下一个案例。

此外,您的任何switch语句中都没有"Goblin"case

当您调用defaultStartingPoints() ,它会随机化统计数据,以便智慧统计数据不再是[0, 1, 1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM