IF语句,不确定为什么它不能正常工作C#

时间:2017-10-02 18:33:47

标签: c#

我一直试图解决这个问题。 IF语句用于检查多个条件,而不是通过else运行4次。计时器应在足球比赛中进行半场检查,并且如果它与任何时间相匹配,则链接到其称为半场或全场的裁判级别。任何帮助是极大的赞赏。以下是我当前的代码版本:

if (diceRoll == 1)
{
    timer[0] = timer[0] + 10; //Adds 10 minutes to the timer

    if (timer[0] == 45 & timer[0] == 50 & timer[0] == 90 & timer[0] == 95) // Checks for half time
    {
        Referee.timerCheck();
    }
    //Continues if it is not half time or full time
    Console.WriteLine(teams[0] + " have lost possession of the ball. It now sits with " + teams[1] + " in the left centre midfield!");
    Console.WriteLine(timer[0] + " minutes have been played.");
    Console.WriteLine("Press <ENTER> to roll the dice to see the outcome!");
    Console.ReadLine();
    ALCM();
}
else if (diceRoll == 2)
{
    Console.WriteLine(teams[0] + " have passed the ball back into the right centre back position!");
    Console.WriteLine("Press <ENTER> to roll the dice to see the outcome!");
    Console.ReadLine();
    HRCB();
}
else if (diceRoll == 3)
{
    Console.WriteLine(teams[0] + " have passed the ball accross into onto the right winger who has a chance to shoot or pass!");
    Console.WriteLine("Press <ENTER> to roll the dice to see the outcome!");
    Console.ReadLine();
    HRW();
}
else if (diceRoll == 4)
{
    Console.WriteLine(teams[0] + " have passed the ball forwards into the right striker!");
    Console.WriteLine("Press <ENTER> to roll the dice to see the outcome!");
    Console.ReadLine();
    HRS();
}
else if (diceRoll == 5)
{
    Console.WriteLine(teams[0] + " have passed the ball forwards into the right striker!");
    Console.WriteLine("Press <ENTER> to roll the dice to see the outcome!");
    Console.ReadLine();
    HRS();
}
else
{
    Console.WriteLine(teams[0] + " have passed the ball forwards into the right striker!");
    Console.WriteLine("Press <ENTER> to roll the dice to see the outcome!");
    Console.ReadLine();
    HRS();
}

2 个答案:

答案 0 :(得分:1)

你真的需要使用|| (逻辑OR)(不需要使用|,因为你的等式没有副作用)

你的陈述应该是:

if (timer[0] == 45 || timer[0] == 50 || timer[0] == 90 || timer[0] == 95)

答案 1 :(得分:0)

好吧,让我们分一小段地说: 首先,你应该为你的diceRoll使用开关。它会使阅读更容易。

infoButton.widthAnchor.constraint(equalToConstant: 25).isActive = true
infoButton.heightAnchor.constraint(equalToConstant: 25).isActive = true

现在,你的如果没有做你期望的事情。您正在使用&amp; 而不是 || (或)。 它应该是:

switch(diceRoll)
{
    case 1:
      //your code
      break;
    case 2:
      //your code
      break;
}