-2

我只是对下面的代码有疑问。if 语句无法正常工作,它们没有正确测试月份。我相信问题出在不等于部分。使用脚本这似乎是问题所在。在这方面,我不是 100% 熟悉 javascript 语法。谢谢

<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);              
            }
            }

4

1 回答 1

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

对于年份的任何值都是正确的,您可能想要

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

你也检查了day > 28两次。

于 2013-06-20T10:47:01.277 回答