2013-06-20 77 views
-2

我只是有一個關於我的代碼如下的問題。 if語句工作不正常,他們沒有正確測試月份。我相信問題在於不等於零件。使用腳本,這是問題似乎出現的地方。在這方面,我不是100%熟悉javascript語法。由於if語句中的多個「ors」和「ands」

<script = text/javascript>   
      function calculate(){ 
      var year = parseInt(document.getElementById("year").value); 
      var month = parseInt(document.getElementById("month").value); 
      var day = parseInt(document.getElementById("day").value); 

      if ((month == 2 && day > 28) && (!(year == 2016) || (!(year == 2020))) && day > 28) 
      { 
       alert("Sorry. The day " + day + " is not available in the month of February in the year of " + year); 
      } 
      else if((month == 2 && day > 29) && ((year == 2016) || (year == 2020))){ 
       alert("Sorry. The day " + day + " is not available in the month of February in the year of " + year); 
      } 
      else 
      { 

      newmonth = month + 9; 
      newday = day + 10; 

      //if the month is greater then 12 roll over the year and change the month 
      if ((newmonth > 12)){ 
       year = year + 1; 
       newmonth = newmonth - 12; 
      } 
      //if the day equals January and has a day grater than 31 then roll over to the next month 
      if ((newday > 31) && (newmonth == 1)){ 
       newmonth = newmonth + 1; 
       newday = newday - 31; 
      } 
      //if the day equals February and has a day grater than 28 on a non leap year then roll over to the next month 
      if ((newday > 28) && (newmonth == 2) && (year == 2014)){ 
        newmonth = newmonth + 1; 
        newday = newday - 28; 
      } 
      //if the day equals February and has a day grater than 29 then roll over to the next month 
      if ((newday > 29) && (newmonth == 2) && (year == 2016)){ 
       newmonth = newmonth + 1; 
       newday = newday - 29; 
      } 
      //if the day equals February and has a day grater than 29 then roll over to the next month 
      if ((newday > 29) && (newmonth == 2) && (year == 2020)){ 
       newmonth = newmonth + 1; 
       newday = newday - 29; 
      } 
      //if the day equals February and has a day grater than 29 then roll over to the next month 
      if ((newday > 29) && (newmonth == 2) && (year == 2024)){ 
       newmonth = newmonth + 1; 
       newday = newday - 29; 
      } 
      //if the day equals February and has a day grater than 29 then roll over to the next month 
      if ((newday > 29) && (newmonth == 2) && (year == 2028)){ 
       newmonth = newmonth + 1; 
       newday = newday - 29; 
      } 
      //if the day equals March and has a day grater than 31 then roll over to the next month 
      if ((newday > 31) && (newmonth == 3)){ 
       newmonth = newmonth + 1; 
       newday = newday - 31; 
      } 
      //if the day equals April and has a day grater than 30 then roll over to the next month 
      if ((newday > 30) && (newmonth == 4)){ 
       newmonth = newmonth + 1; 
       newday = newday - 30; 
      } 
      //if the day equals May and has a day grater than 31 then roll over to the next month 
      if ((newday > 31) && (newmonth == 5)){ 
       newmonth = newmonth + 1; 
       newday = newday - 31; 
      } 
      //if the day equals June and has a day grater than 30 then roll over to the next month 
      if ((newday > 30) && (newmonth == 6)){ 
       newmonth = newmonth + 1; 
       newday = newday - 30; 
      }    
      //if the day equals July and has a day grater than 31 then roll over to the next month 
      if ((newday > 31) && (newmonth == 7)){ 
       newmonth = newmonth + 1; 
       newday = newday - 31; 
      } 
      //if the day equals March and has a day grater than 31 then roll over to the next month 
      if ((newday > 31) && (newmonth == 8)){ 
       newmonth = newmonth + 1; 
       newday = newday - 31; 
      } 
      //if the day equals September and has a day grater than 30 then roll over to the next month 
      if ((newday > 30) && (newmonth == 9)){ 
       newmonth = newmonth + 1; 
       newday = newday - 30; 
      } 
      //if the day equals October and has a day grater than 31 then roll over to the next month 
      if ((newday > 31) && (newmonth == 10)){ 
       newmonth = newmonth + 1; 
       newday = newday - 31; 
      } 
      //if the day equals November and has a day grater than 30 then roll over to the next month 
      if ((newday > 30) && (newmonth == 11)){ 
       newmonth = newmonth + 1; 
       newday = newday - 30; 
      } 
      //if the day equals December and has a day grater than 31 then roll over to the next month 
      if ((newday > 31) && (newmonth == 12)){ 
       newmonth = 1; 
       newday = newday - 31; 
      } 
      alert("Your cow is due to calve on:"+" Day "+newday+" Month "+newmonth+" Year "+year);    
      } 
      } 

+0

99%的時候,我指責一個'如果'我的代碼不正確的工作,原來是我的錯。只是說:) –

+1

JavaScript有一個'Date'對象。你應該使用它而不是編寫這麼多的代碼。請參閱https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date。 –

回答

1
(!(year == 2016) || (!(year == 2020))) 

將爲真爲一年中的任何值,你可能想

(!((year == 2016) || (year == 2020))) 

你也檢查day > 28兩次。

+0

謝謝你的幫助,完美的工作。並感謝所有人的幫助 – user1441970