创建的方法无法正常工作

时间:2018-06-05 17:18:44

标签: c# unity3d

这是我的代码

public void StringToInt(int[] arrPart, int temp , string[] arrPartStr)
{
    for (int c = 0; c < arrPart.Length; c++)
    {
        temp = 0;

        if (arrPartStr[c][0] == 'P') temp = PLAYER * 100;
        else if (arrPartStr[c][0] == 'B') temp = BANKER * 100;
        else temp = TIE * 100;
        if (arrPartStr[c][1] == 'P') temp += PLAYERBANKER;
        if (arrPartStr[c][2] == 'P') temp += BANKERPLAYER;

        arrPart[c] = temp;
    }
}

public static string SplitString(string history)
{
    string[] text = history.Split(',');

    return history;
}

现在我在这里应用我创建的所有方法

public void firstMethod(string history)
{
    string[] arrPartStr = new string[] { SplitString(history) };
    int[] arrPart = new int[arrPartStr.Length];
    int temp = 0;
    StringToInt(arrPart, temp, arrPartStr);
}

现在发生的事情是firstMethod()内的代码无法正常工作我是怎么说的。因为它没有得到我想要的预期输出。但是以下代码

public void firstMethod(string history)
{
    string[] arrPartStr = history.Split(',');
    int[] arrPart = new int[arrPartStr.Length];

    for (int c = 0; c < arrPart.Length; c++)
    {
        int temp = 0;

        if (arrPartStr[c][0] == 'P') temp = PLAYER * 100;
        else if (arrPartStr[c][0] == 'B') temp = BANKER * 100;
        else temp = TIE * 100;
        if (arrPartStr[c][1] == 'P') temp += PLAYERBANKER;
        if (arrPartStr[c][2] == 'P') temp += BANKERPLAYER;

        arrPart[c] = temp;
    }
}

当我直接将代码放在firstMethod()中时,效果非常好。有人可以告诉我为什么。

1 个答案:

答案 0 :(得分:2)

您的splitstring函数返回history而不是text;换句话说,您拆分字符串,然后返回原始字符串而不是新的拆分对象。相反,返回如下文本:

public static string[] SplitString(string history){

    string[] text = history.Split (',');

    return text;
}

然后像这样称呼它

string[] arrPartStr = SplitString (history);

而不是

string[] arrPartStr = new string[] {SplitString (history)};