Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何生成随机颜色?_C#_Arrays_Colors - Fatal编程技术网

C# 如何生成随机颜色?

C# 如何生成随机颜色?,c#,arrays,colors,C#,Arrays,Colors,所以我在c#中胡闹,想知道如何从数组中生成字符串,但使用随机颜色: while (true) { string[] x = new string[] { "", "", "" }; Random name = new Random(); Console.WriteLine((x[name.Next(3)])); Thread.Sleep(100); } 当我输

所以我在c#中胡闹,想知道如何从数组中生成字符串,但使用随机颜色:

    while (true)
        {
            string[] x = new string[] { "", "", "" };
            Random name = new Random();
            Console.WriteLine((x[name.Next(3)]));
            Thread.Sleep(100);
        }
当我输出x时,我希望它是随机颜色。
谢谢

如果您想使用标准控制台颜色,您可以将和混合,以获得随机颜色。然后使用和/或更改控制台的颜色

// 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);
}

再多解释一下就好了。例如,您是否期望
“红色”
“FF0000”
颜色的
颜色。红色或。。?是否有一组可能的值列表,或者您是否希望为R、G和B组件生成随机数,然后获取相应的
Color
对象(不一定使用漂亮的英文名称)?Console.WriteLine((x[random.Next(3)])是什么;在代码中做什么?代码输入:randonGen->random用户希望以随机颜色输出x数组中包含的随机字符串。Console.WriteLine将输出数组中包含的一个字符串,因为它是随机的。接下来(3)将生成一个介于0和2之间的数字,它将表示数组中所需字符串的位置。因此,是随机的。下一步(3)返回例如0,数组中的第一个字符串将在控制台中写入。我想从数组(?)生成一种颜色,我想让它随机化,例如ConsoleColor.Red或ConsoleColor.Green。由于某种原因,上面的代码无法正常工作,我在ColorColor.FromArgb上遇到一个错误。该错误无法隐式地将类型System.Drawing.Color转换为System.ConsoleColor。我注意到上面的代码从未设置控制台颜色,它只是检索了一个随机颜色。此外,还有一些不匹配的变量名。我已经修复了代码片段。
// 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);
}