2016-11-04 47 views
0

我想让用户选择何时完成输入,所以我初始化“int完成”。但是循环绕过了。 我试图同时,do-while循环,但它不会为做我我的循环不会完成

char snails[8][11]; 

int count; 
int finish; 
do 
{ 
    for (count = 0; count < 8; count++) 
    { 
     printf("Enter the snail's name:"); 
     scanf("%10s", &snails[count]); 
     int timea; 
     int timeb; 
     int timec; 
     int timed; 

     printf("Enter %s 's time for the first leg:", snails[count]); 
     scanf("%d", &timea); 
     printf("Enter %s 's time for the second leg:", snails[count]); 
     scanf("%d", &timeb); 

     printf("Enter %s 's time for the third leg:", snails[count]); 
     scanf("%d", &timec); 
     printf("Enter %s 's time for the fourth leg:", snails[count]); 
     scanf("%d", &timed); 

     int timef = timea + timeb + timec + timed; 

     int time1 = timef/60; 
     int time2 = timef % 60; 

     printf("%s finished in %d minutes and %d seconds \n", snails[count], time1, time2); 
     printf("Type 1 if you have finished inputting or 0 if you have not:"); 
     scanf("%d", &finish); 
    } 


} while (finish == 0); 


return 0; 

}

+1

Nitpick:'scanf(“%10s”,&snails [count]);'应该是'scanf(“%10s”,snails [count]);'。 – haccks

+0

因为直到'do'-'while'循环结束时它才检查条件。尝试'count <8 && finish!= 0'作为'for'循环中的条件并且消除'do'-'while'。 –

+0

谢谢我不得不添加&&完成== 0到“count <8” –

回答

0

如果你的目的是要执行循环的8倍,除非他们表明他们完成:

1.删除do while while循环。

2.Do下列操作之一: 改变的条件下进行的面貌:

count < 8 && finish != 1 

或脱离for循环:

if (finish == 1) break; 

那么它将执行for循环直到:

  • 它这样做8次
  • ,或者询问是否完成时输入1