Hea*_*r T 0 c++ if-statement while-loop
几乎就是标题所说的.我是这个彩票号码分配的最后一点,我不知道为什么我调试时第二组if/else if语句没有显示.我知道if/else语句是互斥的 - 但是如果两个测试都不应该是if和then 这是代码.
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++;
}
Run Code Online (Sandbox Code Playgroud)
我知道为了简单起见我可能应该用for循环替换while循环但是我对此更加舒服.
count永远不会等于5你的if-else条件需要它的while循环内部.如果你在那count 之前增加了,if-else那么就可以满足其中一个条件(取决于它的值tally).