我有一个作业,用户在提示框中输入一个数字,月份就出来了。到目前为止,这是代码:
<script type="text/javascript">
var a = prompt("enter a month number please.");
var b = "";
switch(a){
case 1: b = "January";
break;
case 2: b = "February";
break;
case 3: b = "March";
break;
case 4: b = "April";
break;
case 5: b = "May";
break;
case 6: b = "June";
break;
case 7: b = "July";
break;
case 8: b = "August";
break;
case 9: b = "September";
break;
case 10: b = "October";
break;
case 11: b = "November";
break;
case 12: b = "December";
break;
}
if((a==12) || (a==1) || (a==2)){
document.write(" It is " + a + ", which is in winter.")
}
if((a==3) || (a==4) || (a==5)){
document.write(" It is " + a + ", which is in spring.")
}
if((a==6) || (a==7) || (a==8)){
document.write(" It is " + a + ", which is in summer.")
}
if((a==9) || (a==10) || (a==11)){
document.write(" It is " + a + ", which is in fall.")
}
</script>
我的月份不是输出。相反,这个数字是我的输出。似乎我的switch语句被忽略,只做if语句。我迷失了我的错误。
答案 0 :(得分:8)
您正在使用包含数字的变量而不是包含该字符串的变量。在输出中使用b
:
document.write(" It is " + b + ", which is in winter.");
您也可以将代码编写为:
var a = parseInt(prompt("enter a month number please."), 10);
var month = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var b = month[a - 1];
var season;
switch (a) {
case 12:
case 1:
case 2: season = "winter"; break;
case 3:
case 4:
case 5: season = "spring"; break;
case 6:
case 7:
case 8: season = "summer"; break;
case 9:
case 10:
case 11: season = "fall"; break;
}
document.write(" It is " + b + ", which is in " + season + ".");
答案 1 :(得分:2)
b
包含不是a
的月份字符串,例如
document.write(" It is " + b + ", which is in fall.")
答案 2 :(得分:0)
document.write(" It is " + a + ", which is in winter.")
您在document.write
中使用了“a”而不是“b”答案 3 :(得分:0)
效率更高:
months = [["January",0],["February",0],["March",1]]; // Too tired to finish this array.
season = ["winter","spring","summer","fall"];
var a = prompt("Enter a month number please.");
if(!a.isNaN)
{
document.write(" It is " + months[a][0] + ", which is in "+season[months[a][1]] +".")
}
列昂
答案 4 :(得分:0)
数学也很酷:
var a = prompt("enter a month number please.");
var months = [
"December", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November"
];
var seasons = ["winter", "spring", "summer", "fall"];
if (a == 12) a = 0;
document.write(" It is " + months[a] + ", which is in " + seasons[Math.floor(a/3)] + ".");
或者你可以写:
var a = prompt("enter a month number please.") % 12; // december is now 0, each other month remains the same.
var months = [
"December", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November"
];
var seasons = ["winter", "spring", "summer", "fall"];
document.write(" It is " + months[a] + ", which is in " + seasons[Math.floor(a/3)] + ".");
答案 5 :(得分:0)
我试着评论Guffa的答案,但我不能因为我的排名很低。 user1725798试试这个。提示返回一个字符串,您最初是在与一个数字进行比较。你需要解析字符串。
var a = parseInt(prompt("enter a month number please."));
if (!a) {
return;
}
var month = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var b = month[a - 1];
var season;
switch (a) {
case 12:
case 1:
case 2: season = "winter"; break;
case 3:
case 4:
case 5: season = "spring"; break;
case 6:
case 7:
case 8: season = "summer"; break;
case 9:
case 10:
case 11: season = "fall"; break;
}
document.write(" It is " + b + ", which is in " + season + ".");