Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在控制台中对齐同一行上的项目?_C#_Arraylist_Console - Fatal编程技术网

C# 如何在控制台中对齐同一行上的项目?

C# 如何在控制台中对齐同一行上的项目?,c#,arraylist,console,C#,Arraylist,Console,所以我正在做一个“输入正确的单词到混淆的单词”的游戏来练习一些c#,因为我对它还很陌生。最后,我想输出三列中的所有单词,但因为我正在一个接一个地遍历我的所有3个列表,它迭代的下一个列表将在前一个列表之后输出 以下是控制台输出: 这是我的密码: String s = String.Format("{0, -10} {1, -10} {2, -10}\n\n","The mixed word: ","The correct word: ", "Your input: "); index = 0;

所以我正在做一个“输入正确的单词到混淆的单词”的游戏来练习一些c#,因为我对它还很陌生。最后,我想输出三列中的所有单词,但因为我正在一个接一个地遍历我的所有3个列表,它迭代的下一个列表将在前一个列表之后输出

以下是控制台输出:

这是我的密码:

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}");
String s=String.Format(“{0,-10}{1,-10}{2,-10}\n\n”,“混合词:”,“正确词:”,“您的输入:”);
指数=0;
而(指数<3)
{
maxval=0;
while(maxval2)
{
打破
}
maxval++;
}
}
索引++;
}
Console.Write($“{s}”);

我希望所有三个列表都在同一高度/行上。

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

for(var index = 0; index < words.TheList[0].Count; index++)
{
    Console.Write($"{words.TheList[0][index]} {words.TheList[1][index]} {words.TheList[2][index]}");
}
for(var index=0;index
假设三个列表长度相同,下面是一个解决方案:

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}");
for(int i=0;i
我不能使用.Length,因为我的列表包含另一个列表。这是错误消息所说的。当您将
.Length
替换为
.Count
时会发生什么情况。我不能使用.Length,因为我的列表包含另一个列表。这是错误消息所说的。“'List'不包含'Length'的定义,并且找不到接受'List'类型的第一个参数的可访问扩展方法'Length'(是否缺少using指令或程序集引用?)“啊,是的,这种情况经常发生。对列表使用
Count
,对数组使用
Length
。这是一个很好的例子,说明了为什么您的代码示例应该是完整的。理想情况下,我们应该能够将其复制并粘贴到代码编辑器中并对其进行测试,否则我们只是在编写我们希望有效的代码!哦,好的。。。下一次我将粘贴整个代码。再次强调Thx。如果你发布的内容太多或太少,人们会抱怨,知道要包含多少内容可能会很棘手。理想情况下,它需要是最小的和完整的,以得到一个好的答案。