Switch语句在课堂上不起作用

时间:2014-07-01 09:37:39

标签: c#

我正在开发一个基于文本的RPG,并且我正在实施一个统计系统。我与它交互的类的代码是:

switch (charClass)
        {
            case "a":
                Player.charC(0);
                break;
            case "b":
                Player.charC(1);
                break;
            case "c":
                Player.charC(2);
                break;
        }

public static void charC(int charPath)
    {
        switch (charPath)
        {
            case 0:
                Player.stats(0);
                break;
            case 1:
                Player.stats(1);
                break;
            case 2:
                Player.stats(2);
                break;
        }
    }

 public static void stats(int stat)
    {

        Player p = new Player();

        switch (stat)
        {
            case 0:
                p.m_health = 200;
                p.m_mana = 75;
                p.fast = 7;
                p.strng = 20;
                p.smrt = 7;
                p.move = 7;
                p.level = 1;
                break;
            case 1:
                p.m_health = 100;
                p.m_mana = 200;
                p.fast = 10;
                p.strng = 7;
                p.smrt = 15;
                p.move = 7;
                p.level = 1;
                break;
            case 2:
                p.m_health = 100;
                p.m_mana = 100;
                p.fast = 10;
                p.strng = 10;
                p.smrt = 10;
                p.move = 10;
                p.level = 1;
                break;
        }
    }

当我尝试运行它时,它会将所有统计信息返回为0。

示例:MAX HEALTH 0. MAX MANA 0. FAST 0. STRONG 0. SMART 0。

2 个答案:

答案 0 :(得分:0)

stats方法中,您没有返回播放器对象。您创建一个新的播放器,设置其属性,但丢弃方法末尾的对象。

简而言之:您不会处理您认为自己正在处理的对象。


为什么这一切都是static

答案 1 :(得分:0)

您需要移除统计数据的静态并使用播放器实例调用它

public void stats(int stat)
{
    switch (stat)
    {
        case 0:
            m_health = 200;
            m_mana = 75;
            fast = 7;
            strng = 20;
            smrt = 7;
            move = 7;
            break;
        case 1:
            m_health = 100;
            m_mana = 200;
            fast = 10;
            strng = 7;
            smrt = 15;
            move = 7;
            break;
        case 2:
            m_health = 100;
            m_mana = 100;
            fast = 10;
            strng = 10;
            smrt = 10;
            move = 10;
            break;
    }
    level = 1;
}

在两个开关中使用播放器的实例,即

    switch (charClass)
    {
        case "a":
            playerInstance.charC(0);
            break;
        case "b":
            playerInstance.charC(1);
            break;
        case "c":
            playerInstance.charC(2);
            break;
    }

public void charC(int charPath)
{
    switch (charPath)
    {
        case 0:
            this.stats(0);
            break;
        case 1:
            this.stats(1);
            break;
        case 2:
            this.stats(2);
            break;
    }
}