c++ - If/else if 不执行,即使在 while 循环中出现条件满足

标签 c++ while-loop if-statement

与标题差不多。我已经完成了这个彩票号码分配的最后一点,我不确定为什么在我调试时没有显示第二组 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/

相关文章:

c - 带有 C 语言函数的简单无限循环

java - 为什么我的 if-else block 从来没有被击中,即使它应该被击中? (只需要另一双眼睛。)

c++ - 将原始数据写入驱动器或写入文件哪个更快?

C++ OpenMP exit while loop inside a parallel for

python - 使用 sleep() 时高效快速的 Python While 循环

ios - Playground 执行失败 : <EXPR>:15:33: error: type 'Int' does not conform to protocol 'BooleanType'

sql - if/case语句使用sql查询问题

c++ 当我将字符串转换为 int 时,int 只是一个随机数?

c++ - 在迭代器上检索最大值

c++ - 使用成员变量作为谓词