Kyle
Kyle

Reputation: 351

C# - Why does it set to magenta every time?

I'm trying to set the console background color to a random color but it always returns magenta. What would I have to change to fix this issue. Thanks!

    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++;
            }
        }
    }
}

Upvotes: 0

Views: 144

Answers (2)

Pontus Magnusson
Pontus Magnusson

Reputation: 651

You have misunderstood the concept of a loop I believe and it may not be the tool for you to use. To randomize colors, you would have to associate them with a number and then pick a number and select the associated color.

The ConsoleColor is an enumeration which means each value is already associated with a number. You can select a random value from the enumeration using the method described in this question.

If you want to specifically only have a few colors from the enumeration, you will have to create your own array of the values you want and select a value from that array.

Here is an example of how to select a random item from an array.

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

Upvotes: 3

NicoRiff
NicoRiff

Reputation: 4883

You are actually setting all colors and last magenta. I think you should use if or case statement if you want to set colors conditionally. If you comment the magenta assignmnent you will always get yellow background

        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
        }

I think that what you need is a case statement:

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

Upvotes: 0

Related Questions