Gogo Dev
Gogo Dev

Reputation: 126

How to align my items on the same line in console?

So I'm making a "type in the correct word to the mixed up word" game to practice some c# as I'm pretty new to it. At the end I'd like to output all the words in three columns, but because I'm looping through all my 3 lists one after eachother, the next list it iterates through, will be outputed after the previous list.

Here is the console-output:

The actual output

Here is my code:

String s = String.Format("{0, -10} {1, -10} {2, -10}\n\n","The mixed word: ","The correct word: ", "Your input: ");
index = 0;

while (index < 3)
{
    maxval = 0;

    while (maxval < words.TheList.Count())
    {
        foreach (var value in words.TheList[index])
        {
            if (index == 0)
            {
                s += String.Format("{0, -10}\n", $"{value}");
            }
            else if (index == 1)
            {
                s += String.Format("{0, -10} {1, -10}\n", null, $"{value}");
            }
            else if (index == 2)
            {
                s += String.Format("{0, -10} {1, -10} {2, -10}\n", null, null, $"{value}");
            }
            else if (index > 2)
            {
                break;
            }

            maxval++;
        }
    }

    index++;
}
Console.Write($"{s}");

I expect all the three lists to be on the same height/line.

Upvotes: 2

Views: 664

Answers (2)

Robin Bennett
Robin Bennett

Reputation: 3231

Instead of printing all the values from one list at a time, print the 'Xth' value from each list on one row.

for(var index = 0; index < words.TheList[0].Count; index++)
{
    Console.Write($"{words.TheList[0][index]} {words.TheList[1][index]} {words.TheList[2][index]}");
}

Upvotes: 2

Precious Uwhubetine
Precious Uwhubetine

Reputation: 3007

Assuming the three lists are of the same length, here is a solution:

for (int i = 0; i < words.TheList[0].Count; i++)
{
    s += String.Format("{0, -10} {1, -10} {2, -10} \n", words.TheList[0][i], words.TheList[1][i], words.TheList[2][i]);
}
Console.Write($"{s}");

Upvotes: 1

Related Questions