如果/else即使在while循环内条件似乎已满足,但仍不执行

If/else if not executing even though it appears conditions are met inside of a while loop

本文关键字:满足 执行 条件 else 循环 while 如果      更新时间:2023-10-16

差不多就是标题所说的。我已经完成了这个彩票号码分配的最后一部分,我不知道为什么在调试时没有显示第二组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块时,

count将永远不会为5

一旦它变成5,条件就会失败,循环就会停止。

count在while循环中的if-else条件需要的点处永远不会等于5。如果在if-else之前增加了count,则可以满足其中一个条件(取决于tally的值(。