Reputation: 369
I'm trying to do a version of the game roulette but I'm stuck at the moment. I have put the numbers 0-36 in a list and from there a pick a random number. Then i put the number in a series of if statements, if the result from modulus operator is == 0 its a black and if its >=1 it's red. But when i come to the green 0 i ran into a problem as it shows both green and black as of the first if statement from the modulus result. So my thought is if i could exclude the number 0 from the first if statement? Or anyone have a better more practical solution for this? Any help would be much appreciated.
Random rnd = new Random();
List<int> number = new List<int>();
for (int i = 0; i < 37; i++)
{
number.Add(i);
}
int r = rnd.Next(number.Count);
if (r %2 == 0)
{
Console.WriteLine("You got black " + "(" + r + ")" + "!");
}
if (r %2 >=1 )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You got a red " + "(" + r + ")" + "!");
Console.ResetColor();
}
else if (r == 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("You got a green " + "(" + r + ")" + "!");
Console.ResetColor();
}
Console.ReadKey();
Upvotes: 1
Views: 1495
Reputation: 139
Move
if ( r == 0){...}
to the top if your else ifs
When that is evaluated first, the remaining else blocks won't be.
Upvotes: 2