从if语句中的模数中排除数字零

时间:2014-12-18 00:39:51

标签: c# if-statement modulus

我正在尝试做一个游戏轮盘赌的版本,但我现在卡住了。我把数字0-36放在一个列表中,并从那里选择一个随机数。然后我将数字放在一系列if语句中,如果模数运算符的结果是== 0则为黑色,如果它的> = 1则为红色。但是,当我来到绿色0时,我遇到了一个问题,因为它显示了绿色和黑色,因为模数结果的第一个if语句。所以我的想法是,如果我可以从第一个if语句中排除数字0?或者任何人都有更好的实用解决方案?任何帮助将不胜感激。

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();

1 个答案:

答案 0 :(得分:2)

移动

if ( r == 0){...}

如果你的其他人

那么到顶部

当首先评估它时,剩下的其他块不会。

相关问题