这个“do..while”循环不起作用

发布于 2024-11-04 22:04:22 字数 769 浏览 2 评论 0原文

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");

我想我写错了。我已经搜索了很多,但似乎找不到它有什么问题。

(两支球队中的一支可以赢得奖杯,并且该球队必须获得 3 场胜利)

*else if(sc1

*否则如果(a>b)

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've 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

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

仅当 ab 为 3 时才会循环。如果您想等到其中之一为三,请使用:

while ((a!=3) && (b!=3));
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));
苄①跕圉湢 2024-11-11 22:04:22

您的循环条件不正确。由于两队得分均未​​达到 3,因此提前停止。循环直到一方得分达到 3:

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));
殤城〤 2024-11-11 22:04:22

基本上,你告诉它在 a 或 b 仍然等于 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))

嘿咻 2024-11-11 22:04:22

如果我正确地阅读你的问题,你希望终止条件是“a”或“b”队之一的得分为 3。但是,在你的代码中,你已经写了唯一的方法如果其中一支球队的得分为 3,则可以循环。您想要:

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) )
硪扪都還晓 2024-11-11 22:04:22

您的条件:

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

声明只要 ab 等于 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文