0

我有一个任务,用户在提示框中输入一个数字,然后月份就出来了。到目前为止,这是代码:

<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 语句。我迷失在我做错了什么。

4

6 回答 6

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 + ".");

演示:http: //jsfiddle.net/Guffa/ZuXmP/

于 2012-12-06T22:52:14.790 回答
2

b包含月份字符串而不是a例如

document.write(" It is " + b + ", which is in fall.")
于 2012-12-06T22:52:15.877 回答
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)] + ".");​
于 2012-12-06T23:17:43.850 回答
0

document.write(" It is " + a + ", which is in winter.")

您在 document.write 中使用了“a”而不是“b”

于 2012-12-06T22:52:59.763 回答
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]] +".")
}

莱昂

于 2012-12-06T22:59:09.840 回答
0

我尝试评论 Guffa 的答案,但由于我的排名较低,我不能。user1725798 试试这个。Prompt 返回一个字符串,您最初是在与一个数字进行比较。您需要解析字符串。

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 + ".");
于 2012-12-06T23:57:59.190 回答