开发者

This `do..while` loop isn't working

开发者 https://www.devze.com 2023-03-01 17:45 出处:网络
int sc1,sc2,a=0,b=0; do { printf(\"Give the scores\\n\"); scanf(\"%d %d\", &sc1,&sc2); //===============================================
int sc1,sc2,a=0,b=0;

do
{

 printf("Give the scores\n");

 scanf("%d %d", &sc1,&sc2);

 //===============================================

 if (sc1 > sc2)     
   a+=1;
 else if (sc1<sc2)
   b+=1;
 else if (sc1==sc2)
   printf("tie\n");

 //===============================================

 if (a>b)    
    printf("team 1 is in the lead\n");
 else if (a<b)
    printf("team 2 is in the lead\n");
 else if (b==a)
    printf("tie\n");      

}
while((a==3) || (b==3));

//===============================================


if (a==3)
  printf("team 1 got the cup");
else
  printf("team 2 got the cup");

I think that I wrote something wrong. I'开发者_StackOverflowve searched it a lot but can't seem to find what is wrong with it.

(One of the two teams can win the cup and that team has to have 3 wins)

*else if(sc1

*else if(a>b)


while((a==3) || (b==3));

will loop only if a or b is three. If you want to wait until one of them is three, use:

while ((a!=3) && (b!=3));


Your loop condition is incorrect. It is stopping early because neither team has a score of 3. Loop until one or the other gets up to 3:

while((a < 3) && (b < 3));


Basically, you're telling it to loop while a or b still equal 3. Which is not what you want. You want while((a<3) && (b<3))


If I'm reading your question properly you'd like the terminating condition to be that one of the teams "a" or "b" has a score of 3. However, in your code you've written that the only way the while can loop is if one of the teams has a score of 3. You want:

while( !( a==3 || b == 3) )


Your conditional:

while((a==3) || (b==3));

States that the loop will continue so long as either a or b are equal to 3. Are you sure this is what you wanted?

0

精彩评论

暂无评论...
验证码 换一张
取 消