控制台背景颜色未重置

问题描述 投票:0回答:1

即使使用 Console.ResetColor();最后一行之后它仍然没有重置

if (Total >= 15)
{
    Console.BackgroundColor = ConsoleColor.Green;
    Console.WriteLine("You win");
    
    if (Total - 15 == 0)
    {
        Console.WriteLine("You won without any extra points, :)");
    }
    else
    {
        Console.WriteLine($"You won with {Total - 15} extra points, :)");
    }
    
    Console.ResetColor();
}
else
{
    Console.BackgroundColor = ConsoleColor.Red;
    Console.WriteLine("You lost");
    Console.WriteLine($"You were {15 - Total} points away, :(");
    Console.ResetColor();
}

Console.BackgroundColor = ConsoleColor.Black;
Console.ResetColor();

重置 if 语句上的颜色并将背景颜色更改为黑色,但什么也不做。

在 PowerShell 终端中测试是否有帮助。

c# terminal console .net-8.0 console.writeline
1个回答
0
投票

试试这个

if (Total >= 15)
{
   Console.BackgroundColor = ConsoleColor.Green;
   Console.WriteLine("You win");

   if (Total - 15 == 0)
   {
       Console.WriteLine("You won without any extra points, :)");
   }
   else
   {
       Console.WriteLine($"You won with {Total - 15} extra points, :)");
   }

   Console.ResetColor();
}
else
{
   Console.BackgroundColor = ConsoleColor.Red;
   Console.WriteLine("You lost");
   Console.WriteLine($"You were {15 - Total} points away, :(");
   Console.ResetColor();
}

Console.BackgroundColor = ConsoleColor.Black;
Console.ResetColor();
© www.soinside.com 2019 - 2024. All rights reserved.