Console Background color doesn't reset

87 Views Asked by At

Even with Console.ResetColor(); it still doesn't reset after the last line

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

Resetting the color on the if statements apart and also changing the background color to black but does nothing as well.

Tested in the PowerShell terminal if that helps.

2

There are 2 best solutions below

2
anis2dev On

Try this one

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();
0
UlisesDev On

apparently restarting my pc did the trick