与标题差不多。我已经完成了这个彩票号码分配的最后一点,我不确定为什么在我调试时没有显示第二组 if/else if 语句。我知道 if/else 语句是相互排斥的——但不应该一个 if 和另一个 if 都测试吗? 这是代码。
count=0;
while(count<5)
{
if(lottery[count] == user[count])
{
lotto = lottery[count];
cout<<"So, you matched numbers with "<<lotto <<".\n";
tally++;
}
if(tally==5 && count == 5)
{
cout<<"Congratulations, you're the grand prize winner!";
}
else if(tally < 5 && count == 5)
{
cout<<"A total of "<<tally<<" of your lottery picks matched.";
}
else if(tally == 0 && count == 5)
{
cout<<"Caution. The following comment is an inside joke. Read at your own risk.";
cout<<"Bet you feel like a total loser huh? Nothing matched.";
}
count++;
}
我知道为了简单起见,我可能应该用 for 循环替换 while 循环,但我更喜欢 while。
最佳答案
当执行 if
block 时,
count
永远不会为5
。
一旦变成5
,条件就失败,循环停止。
关于c++ - If/else if 不执行,即使在 while 循环中出现条件满足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15601341/