2016-11-04 79 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