代码之家  ›  专栏  ›  技术社区  ›  Ginxxx

创建的方法无法正常工作

  •  -1
  • Ginxxx  · 技术社区  · 8 年前

    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;
        }
    }
    

    当我直接将代码放入 第一种方法() .有人能告诉我为什么吗。

    1 回复  |  直到 8 年前
        1
  •  2
  •   Ginxxx    8 年前

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

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

    string[] arrPartStr = SplitString (history);
    

    而不是

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