c# - 如何在控制台中将我的项目对齐在同一行?

标签 c# arraylist console

所以我正在制作一个“在混合单词中输入正确的单词”游戏来练习一些 C#,因为我对它还很陌生。最后,我想输出三列中的所有单词,但因为我依次循环遍历所有 3 个列表,所以它迭代的下一个列表将在上一个列表之后输出。

这是控制台输出:

The actual output

这是我的代码:

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

我希望所有三个列表都位于相同的高度/线上。

最佳答案

不要一次打印一个列表中的所有值,而是将每个列表中的“第 X”个值打印在一行上。

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

关于c# - 如何在控制台中将我的项目对齐在同一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58517719/

相关文章:

c# - Gridview 应用程序中的错误

java - MapReduce ArrayList 类型不匹配

java - 如何管理/列出我的对象?

c# 正则表达式匹配字符串中的精确单词

c# - Visual Studio 2017(版本 15.7.3)- C# : the system cannot find the file specified

c# - 拦截webapi json格式错误

java - 为什么使用 ArrayList 的 Collections.copy 会产生 IndexOutOfBounds 异常?

python - 暂停 Python 控制台

Java应用程序控制台提示符

java - 如何在控制台中使用 Java 创建键绑定(bind)?