C#程序显示System.String []而不是显示字符串

时间:2018-01-12 22:00:21

标签: c# string system

我正在尝试创建一个程序,它接受一个字符列表和一个选择移动列表,并在完成所有移动后显示所选择的结果字符。

问题是我是初学者,我的程序显示System.String []而不是表示字符的字符串。我试图在Microsoft文档中搜索,但我找不到与我的问题相关的任何内容,因此我可以理解为什么会发生这种情况以及我做错了什么。

非常感谢!

以下是我的代码:

static void Main(string[] args)
    {
        string[][] fighters = new string[][]
        {
          new string[] { "Ryu", "E.Honda", "Blanka", "Guile", "Balrog", "Vega" },
          new string[] { "Ken", "Chun Li", "Zangief", "Dhalsim", "Sagat", "M.Bison" }
        };

        string[] moves = new string[] {"up", "left", "right", "left", "left"};

        int[] position = new int[] {0, 0};

        Console.WriteLine(StreetFighterSelection(fighters, position, moves));
        Console.ReadLine();
    }

    static string[] StreetFighterSelection(string[][] fighters, int[] position, string[] moves)
    {
        string[] fighter = new string[] { fighters[position[0]][position[1]] };

        for (int i = 0; i < moves.Length; i++)
        {
            if (moves[i] == "up" && position[0] == 1)
                position[0] -= 1;
            if (moves[i] == "down" && position[0] == 0)
                position[0] += 1;
            if (moves[i] == "right")
            {
                if (position[1] == fighters.GetLength(position[0]))
                {
                    position[1] = 0;
                }   
                else
                {
                    position[1] += 1;
                }
            }
            if (moves[i] == "left")
            {
                if (position[1] == 0)
                {
                    position[1] = fighters.GetLength(position[0]);
                }
                else
                {
                    position[1] -= 1;
                }
            }
            else
                i++;
        }
        return fighter;
    }

1 个答案:

答案 0 :(得分:2)

将对象传递给Console.WriteLine时,将执行该对象的ToString方法。在这种情况下,它显示它是一个字符串[]。您应该迭代字符串数组的字符串对象并显示它们。

foreach(var fighter in StreetFighterSelection(fighters, position, moves))
{
    Console.WriteLine(fighter);
}

或者您可以将字符串加入由逗号分隔的新字符串中:

Console.WriteLine(String.Join(", ", StreetFighterSelection(fighters, position, moves)));