2017-09-16 35 views
0

我需要maxhrmaxmin才能記錄最長時間。在我的if((hr>maxhr)||((hr==maxhr)&&(min>maxmin)));下面是我存儲我的int的地方,但它不斷返回最近輸入的時間。我理解這個問題,但不知道如何解決它。獲取最近時間而不是最長時間

此外,如果聲明,我想說如果當前小時大於我的最大或如果我的小時都相等,但我當前的分鐘大於我的最大。我是否正確表達了這一點?

#include <stdio.h> 
void main() 
{ 
    int hr; 
    int min; 
    int dives; 
    int counter=0; 
    int maxmin=0; 
    int maxhr=0; 
    int totmin=0; 
    int tothr=0; 
    printf("Please enter all dive times, one by one, with hours first and   minutes second, separated by a colon. Ex HH:MM\n"); 
    dives = scanf("%d:%d", &hr,&min); 

    while(dives != EOF) 
    { 
     counter++; 
     tothr = tothr+hr; 
     totmin = totmin+min; 
     if(totmin>59) 
     { 
      totmin = totmin-60; 
      tothr = tothr+1; 
     } 
     if((hr>maxhr)||((hr==maxhr)&&(min>maxmin))); 
     { 
      maxhr = hr; 
      maxmin = min; 
     } 
     dives = scanf("%d:%d", &hr,&min); 
    } 
    printf("The longest dive is %d:%d\n",maxhr,maxmin); 
    printf("The total dive time is %d:%d\n",tothr,totmin); 
} 
+0

你真的不應該在'scanf'後面隱藏'while'。你應該檢查'scanf'返回的內容。 – mch

+2

是否有任何理由不會簡單地將小時和分鐘轉換爲總分鐘數? – EOF

+0

我正在考慮計算總分鐘數,但現在我的控制d並沒有像過去那樣結束程序。另外,我的maxhr = hr是否將hr的值保存到maxhr,或者它是否僅將hr的值與maxhr分享。因爲如果它只是分享,那麼每次我爲hr添加任何新值時,maxhr都不會改變? –

回答

2

提到的聲明,if((hr>maxhr)||((hr==maxhr)&&(min>maxmin)))在最後有一個分號。這可能是你的問題嗎? if語句看起來是正確的。

+0

我刪除了「;」但現在我的控制d不再做任何事情。思考? –

+0

「想法?」 ---現在調試它。 – zerkms