gpt4 book ai didi

c# - 没有背景颜色的控制台文本填充

转载 作者:行者123 更新时间:2023-11-30 15:23:44 24 4
gpt4 key购买 nike

我有一个循环写入一个字符串 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));
}

问题是我的深黄色行从行填充的开始一直被着色。但我只希望单词本身是深黄色,没有空格。

最佳答案

你应该先写没有背景颜色的空格,然后只写有选定背景颜色的单词:

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();

关于c# - 没有背景颜色的控制台文本填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34075533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com