如果要使用标准控制台颜色,可以混合ConsoleColor 枚举和Enum.GetNames()以获得随机颜色。然后,您将使用Console.ForegroundColor和/或Console.BackgroundColor来更改控制台的颜色。
// Store these as static variables; they will never be changing
String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));
int numColors = colorNames.Length;
// ...
Random rand = new Random(); // No need to create a new one for each iteration.
string[] x = new string[] { "", "", "" };
while(true) // This should probably be based on some condition, rather than 'true'
{
// Get random ConsoleColor string
string colorName = colorNames[rand.Next(numColors)];
// Get ConsoleColor from string name
ConsoleColor color = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorName);
// Assuming you want to set the Foreground here, not the Background
Console.ForegroundColor = color;
Console.WriteLine((x[rand.Next(x.Length)]));
Thread.Sleep(100);
}