javascript的if语句嵌套问题

2025-03-25 15:52:00
推荐回答(4个)
回答1:

var today=new Date();
thismonth=today.getMonth()+1;
thisyear=today.getFullYear();
thisday=today.getDate();
var birthyear,birthmonth,birthday,age;
birthyear=prompt("输入你的生日年份");
birthmonth=prompt("输入你的生日月份");
birthday=prompt("输入你的出生日");
age=thisyear-birthyear;
if(thismonth age=age-1;
}else{
if((thismonth==birthmonth)&&(thisday age =age-1;
}
document.write("今天是"+thisyear+"-");
document.write(thismonth+"-"+thisday+"
");
document.write("你的实足年龄是"+age+"岁")
如果是新学js的话,建议保持良好习惯 if加大括号

回答2:

应该是这句

if((thismonth==birthmonth)&&(thisday最后多了;号
多了;号,意思变成
if((thismonth==birthmonth)&&(thisdayage =age-1;
else{document.write("今天是"+thisyear+"-");};
document.write(thismonth+"-"+thisday+"
");
document.write("你的实足年龄是"+age+"岁");

else已经匹配不到if了

回答3:

把If语句后面的分号去掉就可以了,如果不用花括号的话,只能是判断语句后面一条在判断体里面。

回答4:

首先if后没有分号,如果if或者else后存在多行,最好还是加上{},如:
if(){
...

}else if()
{
...

}else{
..

}