简体   繁体   中英

IF statement, not sure why its not working C#

I have been trying to fix this for a while. The IF statement is meant to check multiple conditions instead of running through the else if 4 times. The timer is meant to be checked for half time in a soccer match and if it matches any of the times it links to the referee class where it calls half time or full time. Any help is greatly appreciated. Below is my current version of the code:

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

You really need to be using || (logical OR) (there is no need to use the |, because your equalities have no side effects)

Your statement should be:

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

Well, lets go through this in small pieces: First you should use switch for your diceRoll. It will make it easier to read.

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

Now, your if isn't doing what you expect. You're using & instead of || (or) that is what you want. it should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM