2015-09-03 57 views
0

我還沒有被編程很長,所以我練的dlang一些邏輯練習。關於我在這裏做錯了什麼的想法。當我進入閏年時,我的程序只是在WHILE上循環。閏年沒有日期和時間計劃工作在Dlang

import std.stdio; 
void main() 
{ 
bool dead; 
string thing; 
int phew = 5; //days 
int tahr = 1; //months 
int tron; //monthsDate 
string[7] days = ["Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"]; 
int date = 28; 
string[12] months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; 
int year = 1996; 
int hours = 11; 
int mins = 28; 
string ampm = "pm"; 
bool ly; 
int leap = 1996; 
int cen = 996; 
//writeln("This program is incomplete. Obviously."); 
write("Press Enter to Continue."); 
readf("%s\n",&thing); 
while(!dead) 
{ 
    while(hours <= 12) 
    { 
     while(mins <= 59) 
     { 
      if(mins < 10) 
       write(date," ",months[tahr],", ",year,". ",days[phew],". ",hours,":0",mins,ampm,": "); 
      else 
       write(date," ",months[tahr],", ",year,". ",days[phew],". ",hours,":",mins,ampm,": "); 
      readf("%s\n",&thing); 
      mins++; 
     } 
     hours++; 
     if(hours == 12 && ampm == "am") 
     { 
      ampm = "pm"; 
     } 
     else if (hours == 12 && ampm == "pm") 
     { 
      ampm = "am"; 
      phew++; 
      date++; 
      if(phew > 6) 
       phew = 0; 
      if((date == 29 || date == 30) && tahr == 1) 
      { 
       while(leap <= year) //this assuming time travel doesn't work 
       { //reminder: add time travel 
        if (leap == year) 
        { 
         ly = true; 
         break; 
        } 
        leap+=4; 
        ly = false; 
       } 
       if(!ly || date == 30) 
       { 
        date = 31; 
        leap-=4; 
       } 
      } 
       if(!ly || date == 30) 
       { 
        date = 31; 
        leap-=4; 
       } 
      } 
      if(date == 31 && (tahr == 1 || tahr == 3 || tahr == 5 || tahr == 8 || tahr == 10)) 
      { 
       date = 1; 
       tahr++; 
      } 
      else if (tahr == 11 && date == 32) 
      { 
       tahr = 0; 
       date = 1; 
       year++; 
       cen++; 
       if(cen == 1000) 
       { 
        writeln("Happy Millennium!"); 
        cen = 0; 
       } 
       else 
        writeln("Happy New Year!"); 
      } 
      else if(date == 32 && (tahr == 0 || tahr == 2 || tahr == 4 || tahr == 6 || tahr == 7 || tahr == 9)) 
      { 
       date = 1; 
       tahr++; 
      } 
     } 

     if(hours == 13) 
     { 
      hours = 1; 
     } 
     mins = 0; 
    } 
} 
} 

最重要的部分是這樣的:

if((date == 29 || date == 30) && tahr == 1) 
      { 
       while(leap <= year) //this assuming time travel doesn't work 
       { //reminder: add time travel 
        if (leap == year)/// 
        { 
         ly = true; 
         break; 
        } 
        leap+=4; 
        ly = false; 
       } 
       if(!ly || date == 30) 
       { 
        date = 31; 
        leap=-4; 
       } 
      } 

所以,我發佈後幾乎立刻想通了這個問題。基本上,我寫了= +而不是+ =。非常簡單的錯誤。這就是我輸入得太快的原因。所以,我現在已經修復了代碼,如果您有任何其他建議,請務必將它們放入註釋中。謝謝。

回答

2

決定打印出來躍入終端,我想通了,我的問題。跳躍持續等於4,因爲我寫了= +而不是+ =。這只是一個首先點擊錯誤按鈕的情況。這正是我打字速度快的原因。據我所知,該程序現在可以工作。隨時糾正我對你可能會注意到的任何事情。

0

我沒有深入閱讀,但我建議你添加大量的調試writelns的。 特別是剛剛裏面你想塊應該得到執行:

if((date == 29 || date == 30) && tahr == 1) { 
    writefln("Handling leap years..."); 
    while(leap <= year) //this assuming time travel doesn't work 
    { //reminder: add time travel 
     writefln("In loop, Leap <= year? %s", leap <= year); 

這將讓你更調試運行程序時什麼是實際發生的事情。

+0

是的。這就是我最終做的,現在我已經找到了這個問題。 謝謝^ _ ^。 – fdfgsdfgsdfg