-4

I am trying to make a program that takes a character list and a list of selection moves and after making all the moves to display the resulted character selected.

The problem is that I am a beginner and my program displays System.String[] instead of the string representing the character. I tried to search in the Microsoft documentation but I haven't found anything related to my problem so I can understand why this is happening and what I'm doing wrong.

Thank you very much!


Below is my code:

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
  • string[] is an array of strings. You could iterate through the array, or only write one element. Commented Jan 12, 2018 at 22:03

1 Answer 1

1

When you pass an object to Console.WriteLine the ToString method of the object is executed. In this case, it shows it's a string[]. You should iterate over the string objects of the string array and display them.

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

Or you could join the strings into a new string separated by comma:

Console.WriteLine(String.Join(", ", StreetFighterSelection(fighters, position, moves)));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, ideed using the foreach solved the problem

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.