[英]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. IF 语句旨在检查多个条件,而不是通过 else if 运行 4 次。 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.好吧,让我们小部分讨论一下:首先,你应该为你的 diceRoll 使用switch 。 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.现在,您的if没有按照您的预期进行。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.