老师,我想请问一下我这个作业哪里出错了?为什么最后输出的结果是NaN
<!DOCTYPE html>
<html>
<head>
<title>作业题</title>
<meta charset="utf-8">
<script>
function a(){
var x=(prompt('请输入您的出生年份'));
if(isNaN(x)){
alert('请输入数字');
}else if(x.length!=4){
alert('请输入四位数字');
}else{
var y=parseInt(prompt('请输出您的出生月份'));
var m1,m3,m5,m7,m8,m10,m12=31; //每个月份的天数
var m4,m6,m9,m11=30; //每个月份的天数
if(x%4==0){
var m2=29;
}else{
var m2=28;
}
switch(y){
case 1:
var sum1=0;//月份天数的总和
break;
case 2:
var sum1=m1;
break;
case 3:
var sum1=m1+m2;
break;
case 4:
var sum1=m1+m2+m3;
break;
case 5:
var sum1=m1+m2+m3+m4;
break;
case 6:
var sum1=m1+m2+m3+m4+m5;
break;
case 7:
var sum1=m1+m2+m3+m4+m5+m6;
break;
case 8:
var sum1=m1+m2+m3+m4+m5+m6+m7;
break;
case 9:
var sum1=m1+m2+m3+m4+m5+m6+m7+m8;
break;
case 10:
var sum1=m1+m2+m3+m4+m5+m6+m7+m8+m9;
break;
case 11:
var sum1=m1+m2+m3+m4+m5+m6+m7+m8+m9+m10;
break;
default:
var sum1=m1+m2+m3+m4+m5+m6+m7+m8+m9+m10+m11;
}
var z=parseInt(prompt('请输出您的出生日期'));
sum2=z;
}
var sum=sum1+sum2; //出生的天数总和
var n=parseInt(x);
document.write('您的生日在'+n+'年是第'+sum+'天');
}
a();
</script>
</head>
<body>
</body>
</html>
正在回答
老师修改的代码是没有问题的,可以粘贴测试下:
<!DOCTYPE html> <html> <head> <title>hello</title> <meta charset="utf-8"> <script type="text/javascript"> function a() { var x = (prompt('请输入您的出生年份')); if (isNaN(x)) { alert('请输入数字'); } else if (x.length != 4) { alert('请输入四位数字'); } else { var y = parseInt(prompt('请输出您的出生月份')); var m1 = 31, m3 = 31, m5 = 31, m7 = 31, m8 = 31, m10 = 31, m12 = 31; //每个月份的天数 var m4 = 30, m6 = 30, m9 = 30, m11 = 30; //每个月份的天数 if (x % 4 == 0 && x % 100 !== 0 || x % 400 == 0) { var m2 = 29; } else { var m2 = 28; } switch (y) { case 1: var sum1 = 0; //月份天数的总和 break; case 2: var sum1 = m1; break; case 3: var sum1 = m1 + m2; break; case 4: var sum1 = m1 + m2 + m3; break; case 5: var sum1 = m1 + m2 + m3 + m4; break; case 6: var sum1 = m1 + m2 + m3 + m4 + m5; break; case 7: var sum1 = m1 + m2 + m3 + m4 + m5 + m6; break; case 8: var sum1 = m1 + m2 + m3 + m4 + m5 + m6 + m7; break; case 9: var sum1 = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8; break; case 10: var sum1 = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9; break; case 11: var sum1 = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10; break; default: var sum1 = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11; } var z = parseInt(prompt('请输出您的出生日期')); sum2 = z; } var sum = sum1 + sum2; //出生的天数总和 var n = parseInt(x); document.write('您的生日在' + n + '年是第' + sum + '天'); } a(); </script> </head> <body> </body> </html>
自己再试试。
<!DOCTYPE html>
<html>
<head>
<title>hello</title>
<meta charset="utf-8">
<script type="text/javascript">
function a(){
var x=(prompt('请输入您的出生年份'));
if(isNaN(x)){
alert('请输入数字');
}else if(x.length!=4){
alert('请输入四位数字');
}else{
var y=parseInt(prompt('请输出您的出生月份'));
var m1=31,m3=31,m5=31,m7=31,m8=31,m10=31,m12=31; //每个月份的天数
var m4=30,m6=30,m9=30,m11=30; //每个月份的天数
if(x%4==0 || x%100!==0 || x%400==0){
var m2=29;
}else{
var m2=28;
}
switch(y>=1 && y<=12){
case 1:
var sum1=0;//月份天数的总和
break;
case 2:
var sum1=m1;
break;
case 3:
var sum1=m1+m2;
break;
case 4:
var sum1=m1+m2+m3;
break;
case 5:
var sum1=m1+m2+m3+m4;
break;
case 6:
var sum1=m1+m2+m3+m4+m5;
break;
case 7:
var sum1=m1+m2+m3+m4+m5+m6;
break;
case 8:
var sum1=m1+m2+m3+m4+m5+m6+m7;
break;
case 9:
var sum1=m1+m2+m3+m4+m5+m6+m7+m8;
break;
case 10:
var sum1=m1+m2+m3+m4+m5+m6+m7+m8+m9;
break;
case 11:
var sum1=m1+m2+m3+m4+m5+m6+m7+m8+m9+m10;
break;
default:
var sum1=m1+m2+m3+m4+m5+m6+m7+m8+m9+m10+m11;
}
var z=parseInt(prompt('请输出您的出生日期'));
sum2=z;
}
var sum=sum1+sum2; //出生的天数总和
var n=parseInt(x);
document.write('您的生日在'+n+'年是第'+sum+'天');
}
a();
</script>
</head>
<body>
</body>
</html>
- 参与学习 人
- 提交作业 11218 份
- 解答问题 36713 个
从一个不会编程的小白到一个老司机是需要过程的,首先得入门,学习基础知识,然后才能进阶,最后再到精通,本专题是你走进前端世界的不二选择!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星