【问题标题】:C# - Why does it set to magenta every time?C# - 为什么每次都设置为洋红色?
【发布时间】:2017-02-12 13:39:57
【问题描述】:

我正在尝试将控制台背景颜色设置为随机颜色,但它总是返回洋红色。我必须改变什么来解决这个问题。谢谢!

    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 循环设置的最后一种颜色是洋红色。您可能想要使用不同的构造,例如 ifswitch case
  • 显然您想创建某种Duff's device,希望在while 中的每条指令之后将randomInt 重新评估为&lt; 7,并且它会自动退出@ 987654331@ 发生时。这不是它的工作原理。
  • 您可以尝试播种随机数,这样它就不会总是以相同的序列开始。请参阅此 SO 帖子 stackoverflow.com/questions/4060961/…
  • 另外,在您的 while 循环中,您没有给它时间查看颜色变化。所以它一直到最后,你最终得到了相同的颜色。

标签: c#


【解决方案1】:

我认为您误解了循环的概念,它可能不是您使用的工具。 要随机化颜色,您必须将它们与一个数字关联,然后选择一个数字并选择关联的颜色。

ConsoleColor 是一个枚举,这意味着每个值都已经与一个数字相关联。您可以使用this question中描述的方法从枚举中选择一个随机值。

如果您只想从枚举中指定几种颜色,则必须创建自己的所需值数组并从该数组中选择一个值。

这是一个如何从数组中选择随机项的示例。

Random random = new Random();
ConsoleColor[] colors = new ConsoleColor[] { ConsoleColor.Red, ConsoleColor.Blue };
var myRandomColor = colors[random.Next(0, colors.Length)];

【讨论】:

    【解决方案2】:

    您实际上是在设置所有颜色和最后一个洋红色。如果你想有条件地设置颜色,我认为你应该使用 if 或 case 语句。如果您评论洋红色作业,您将始终得到黄色背景

            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++; //THIS WILL ALWAYS BE YELLOW
            }
    

    我认为你需要的是一个案例陈述:

    switch(randomInt)
    {
         case 0:
            Console.BackgroundColor = ConsoleColor.Red;
            break;
         case 1:
            Console.BackgroundColor = ConsoleColor.Blue;
            break;
         ///....AND SO ON
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      相关资源
      最近更新 更多