[英]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
之前增加了count
, if-else
可以滿足其中一個條件(取決於tally
的值)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.