簡體   English   中英

if / else如果看起來不執行,即使在while循環內滿足條件

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

標題說的差不多。 我到了彩票號碼分配的最后一位,並且不確定在調試時為什么第二組if / else if語句沒有顯示。 我知道if / else語句是互斥的-但是如果兩個都測試,那么不應該再有一個if和else嗎? 這是代碼。

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執行if塊,則count 永遠不會5

一旦變為5 ,條件就會失敗並且循環會停止。

if-else條件要求的時候,while循環內的count永遠不會等於5 如果在if-else 之前增加了countif-else可以滿足其中一個條件(取決於tally的值)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM