这个“do..while”循环不起作用
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
仅当
a
或b
为 3 时才会循环。如果您想等到其中之一为三,请使用:will loop only if
a
orb
is three. If you want to wait until one of them is three, use:您的循环条件不正确。由于两队得分均未达到 3,因此提前停止。循环直到一方得分达到 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:
基本上,你告诉它在 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))
如果我正确地阅读你的问题,你希望终止条件是“a”或“b”队之一的得分为 3。但是,在你的代码中,你已经写了唯一的方法如果其中一支球队的得分为 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:
您的条件:
声明只要
a
或b
等于 3,循环就会继续。您确定这是您想要的吗?Your conditional:
States that the loop will continue so long as either
a
orb
are equal to 3. Are you sure this is what you wanted?