编写一个程序,检查任意一个年份是否是闰年。
如果一个年份可以被4整除不能被100整除,或者可以被400整除,这个年份就是闰年。 并且输入相应年的月份提示这个月有几天。
var years = +prompt('请输入年份');/* 在prompt前加一个“+”可以将输入类型变成整数 */
if (years % 4 === 0 && years % 100 != 0 || years % 400 === 0) {
alert(years + '年是润年');
var month = +prompt('请输入月份');
switch (month) {/* 变量与要匹配的值数据类型要一样 */
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
alert(month + '月有31天');
break;
case 2:
alert(month + '月有29天');
break;
case 4:
case 6:
case 9:
case 11:
alert(month + '月有30天');
break;
default:
alert('输入有误!');
}
} else {
alert(years + '年是平年');
var month = +prompt('请输入月份');
switch (month) {/* 变量与要匹配的值数据类型要一样 */
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
alert(month + '月有31天');
break;
case 2:
alert(month + '月有28天');
break;
case 4:
case 6:
case 9:
case 11:
alert(month + '月有30天');
break;
default:
alert('输入有误!');
}
}