if语句中有多个“ors”和“ands”

时间:2013-06-20 10:39:17

标签: javascript if-statement web

我对下面的代码有疑问。 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);              
            }
            }

1 个答案:

答案 0 :(得分:1)

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

对于任何一年的价值都是如此,你可能想要

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

另外,您检查day > 28两次。

相关问题