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场胜利)

*否则如果(sc1

*否则如果(a>b)

4

5 回答 5

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

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

while ((a!=3) && (b!=3));
于 2011-05-01T19:45:28.677 回答
2

您的循环条件不正确。提前停止是因为两支球队的得分都不是 3。循环直到其中一方得分达到 3:

while((a < 3) && (b < 3));
于 2011-05-01T19:45:59.780 回答
2

基本上,您是在告诉它在 a 或 b 仍等于 3 时循环。这不是您想要的。你要while((a<3) && (b<3))

于 2011-05-01T19:46:23.343 回答
1

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

while( !( a==3 || b == 3) )
于 2011-05-01T19:44:46.330 回答
1

你的条件:

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

声明只要其中一个ab等于 3,循环就会继续。你确定这是你想要的吗?

于 2011-05-01T19:45:02.413 回答