C# 不带背景色的控制台文本填充

C# 不带背景色的控制台文本填充,c#,console-application,padding,background-color,C#,Console Application,Padding,Background Color,我有一个循环,它将字符串写入10次,每次都使用较大的填充。 它还将每隔一行的背景色设置为深黄色 int x = 5; for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { Console.BackgroundColor = ConsoleColor.DarkYellow; } else { Console.ResetColor(); } x = x + 1

我有一个循环,它将字符串写入10次,每次都使用较大的填充。 它还将每隔一行的背景色设置为深黄色

int x = 5;
for (int i = 1; i <= 10; i++)
{
    if (i % 2 == 0)
    {
        Console.BackgroundColor = ConsoleColor.DarkYellow;
    }
    else
    {
        Console.ResetColor();
    }

    x = x + 1;
    string str = "word";
    Console.WriteLine(str.PadLeft(x));
}
intx=5;

对于(int i=1;i这只是在没有任何测试的情况下进行的黑客攻击,但应该给出如何使其工作的想法:

int x = 5;
for (int i = 1; i <= 10; i++)
{
    Console.ResetColor();
    x = x + 1;
    Console.Write("".PadLeft(x));
    if (i % 2 == 0)
        Console.BackgroundColor = ConsoleColor.DarkYellow;

    string str = "word";
    Console.WriteLine(str);
}
intx=5;

对于(int i=1;i你应该先写不带背景色的空格,然后只写带选定背景色的单词:

int x = 5;
for (int i = 1; i <= 10; i++)
{
    Console.ResetColor();
    if (x > 5)
    {
        Console.Write(new String(' ', x - 5));
    }

    if (i % 2 == 0)
    {
        Console.BackgroundColor = ConsoleColor.DarkYellow;
    }

    x = x + 1;
    string str = "word";
    Console.WriteLine(str);
}
Console.ReadLine();
intx=5;
对于(int i=1;i 5)
{
Write(新字符串(“”,x-5));
}
如果(i%2==0)
{
Console.BackgroundColor=ConsoleColor.DarkYellow;
}
x=x+1;
string str=“word”;
控制台写入线(str);
}
Console.ReadLine();
如果在更改颜色之前进行填充会发生什么?请查看
控制台。写入(字符串值)
而不仅仅是
控制台。写入线(字符串值)