Javascript “多重”;ors";及;“和”;在if语句中

Javascript “多重”;ors";及;“和”;在if语句中,javascript,if-statement,web,Javascript,If Statement,Web,我只是对下面的代码有个问题。if语句工作不正常,表示测试不正确。我相信问题出在不相等的部分。使用脚本,这就是问题所在。在这方面,我不是100%熟悉javascript语法。谢谢 <script = text/javascript> function calculate(){ var year = parseInt(document.getElementById("year").value);

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

函数计算(){
var year=parseInt(document.getElementById(“year”).value);
var month=parseInt(document.getElementById(“month”).value);
var day=parseInt(document.getElementById(“day”).value);
如果((月==2日和日>28日)和(!(年==2016年)和(!(年==2020年))和日>28日)
{
警报(“对不起,“+年”中的2月份“+日+”不可用);
}
如果((月==2日和日>29日)和((年==2016年)| |(年==2020年))){
警报(“对不起,“+年”中的2月份“+日+”不可用);
}
其他的
{
newmonth=month+9;
newday=day+10;
//如果月份大于12,则滚动该年并更改月份
如果((新月份>12)){
年份=年份+1;
newmonth=newmonth-12;
}
//如果这一天等于1月,并且一天大于31,则转入下一个月
如果((newday>31)和&(newmonth==1)){
newmonth=newmonth+1;
newday=newday-31;
}
//如果这一天等于二月,并且在非闰年的一天大于28天,那么就转到下一个月
如果((新日>28)和&(新月==2)和&(年==2014)){
newmonth=newmonth+1;
newday=newday-28;
}
//如果这一天等于2月,并且这一天大于29,那么就转到下一个月
如果((新日>29)和&(新月==2)和&(年==2016)){
newmonth=newmonth+1;
newday=newday-29;
}
//如果这一天等于2月,并且这一天大于29,那么就转到下一个月
如果((新日>29)和&(新月==2)和&(年==2020)){
newmonth=newmonth+1;
newday=newday-29;
}
//如果这一天等于2月,并且这一天大于29,那么就转到下一个月
如果((新日>29)和&(新月==2)和&(年==2024)){
newmonth=newmonth+1;
newday=newday-29;
}
//如果这一天等于2月,并且这一天大于29,那么就转到下一个月
如果((新日>29)和&(新月==2)和&(年==2028)){
newmonth=newmonth+1;
newday=newday-29;
}
//如果这一天等于3月,并且一天大于31,则转入下一个月
如果((新日>31)和&(新月==3)){
newmonth=newmonth+1;
newday=newday-31;
}
//如果这一天等于4月,并且一天大于30天,则转入下一个月
如果((newday>30)和&(newmonth==4)){
newmonth=newmonth+1;
newday=newday-30;
}
//如果一天等于5月,并且一天大于31天,则转入下个月
如果((newday>31)和&(newmonth==5)){
newmonth=newmonth+1;
newday=newday-31;
}
//如果这一天等于6月,并且这一天大于30,那么就转到下一个月
如果((新日>30)和&(新月==6)){
newmonth=newmonth+1;
newday=newday-30;
}               
//如果这一天等于7月,并且一天大于31天,则转入下一个月
如果((新日>31)和&(新月==7)){
newmonth=newmonth+1;
newday=newday-31;
}
//如果这一天等于3月,并且一天大于31,则转入下一个月
如果((newday>31)和&(newmonth==8)){
newmonth=newmonth+1;
newday=newday-31;
}
//如果这一天等于9月,并且这一天大于30,那么就转到下一个月
如果((newday>30)和&(newmonth==9)){
newmonth=newmonth+1;
newday=newday-30;
}
//如果这一天等于10月,并且一天大于31天,则转入下一个月
如果((newday>31)和&(newmonth==10)){
newmonth=newmonth+1;
newday=newday-31;
}
//如果这一天等于11月,并且这一天大于30,那么就转到下一个月
如果((newday>30)和&(newmonth==11)){
newmonth=newmonth+1;
newday=newday-30;
}
//如果这一天等于12月,并且一天大于31天,则转入下一个月
如果((newday>31)和&(newmonth==12)){
newmonth=1;
newday=newday-31;
}
警惕(“您的母牛将于:“+”日“+新日“+”月“+新月“+”年“+年”产犊);
}
}

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

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

你还要检查
day>28
两次。

99%的情况下,我指责
如果我的代码工作不正常,那就是我的错。只是说:)JavaScript有一个
Date
对象。您应该使用它,而不是编写这么多代码。看,谢谢你的帮助
(!((year == 2016) || (year == 2020)))