我试图将控制台背景颜色设置为随机颜色,但始终返回洋红色。我需要改变什么来解决这个问题。谢谢!C# - 为什么每次都设置为洋红色?
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Random random = new Random();
int randomInt = random.Next(0, 6);
while(randomInt < 7)
{
Console.BackgroundColor = ConsoleColor.Red;
randomInt++;
Console.BackgroundColor = ConsoleColor.Blue;
randomInt++;
Console.BackgroundColor = ConsoleColor.Cyan;
randomInt++;
Console.BackgroundColor = ConsoleColor.Green;
randomInt++;
Console.BackgroundColor = ConsoleColor.Red;
randomInt++;
Console.BackgroundColor = ConsoleColor.Yellow;
randomInt++;
Console.BackgroundColor = ConsoleColor.Magenta;
randomInt++;
}
}
}
}
使用调试器和步骤通 - 你应该看到你的错误... –
我不明白的问题 - 即得到由'while'循环设置为洋红色最后一种颜色。你可能想要使用不同的结构,例如'if'或'switch'' case' – UnholySheep
显然你想创建某种[Duff's设备](https://en.wikipedia.org/wiki/Duff% 27s_device),希望'randomInt'在'while'中的每条指令之后被重新评估为'<7',并且它会在发生时自动退出'while'。这不是它的工作原理。 – GSerg