javascript switch-statement年龄范围

时间:2017-06-16 13:51:30

标签: javascript switch-statement

坚持使用javascript开关分配。我对这些东西还是比较新的,但似乎无法在我的课本或网上找到任何东西来让这个程序工作。任务是切换if语句切换代码。任何帮助或建议将不胜感激。

var myAge = prompt("Enter your age:", "30");

switch (myAge) {

  case myAge! >= 0:
  case myAge <= 10:
    document.write("myAge is NOT between 0 and 10 <br />");

  case myAge >= 0:
  case myAge <= 10:
    document.write("myAge is between 0 and 10 ");
    break;

  case myAge >= 80:
  case mmyAge <= 10:
    document.write("myAge is 80 or above or 10 or below ");
    break;

  case myAge >= 30:
  case myAge <= 39:
    document.write("myAge is between 30 and 39 ");
    break;

  case myAge >= 80:
  case myAge <= 89:
    document.write("myAge is between 80 and 89 ");
    break;
}

4 个答案:

答案 0 :(得分:0)

请确保您使用的是int而不是字符串,以防万一:

var myAge = parseInt(prompt("Enter your age:", "30"));

并确保将break;语句添加到所有case s。

删除第一个!中的case字符:myAge!myAge

你得到了什么确切的错误?

答案 1 :(得分:0)

在第一个“案例myAge”上,比较运算符不起作用。如果你试图说它不等于,你应该把!=,而不是!&gt; =。

答案 2 :(得分:0)

我认为这可行......但是

------------------------- OLD ANSWER

尝试以下方法;顺便说一下你确定需要document.write

var myAge = ~~prompt("Enter your age:", "30"),
    msg = [];
switch (true) {

    case myAge < 0 && myAge > 10:
        msg.push("myAge is NOT between 0 and 10");

    case myAge >= 0 && myAge <= 10:
        msg.push("myAge is between 0 and 10");

    case myAge >= 80 || myAge <= 10:
        msg.push("myAge is 80 or above or 10 or below");

    case myAge >= 30 && myAge <= 39:
        msg.push("myAge is between 30 and 39");

    case myAge >= 80 && myAge <= 89:
        msg.push("myAge is between 80 and 89");
}
document.write(msg.join('<br />'));

但不是,原因如下: 首先很清楚,你想获得更多的验证消息; 不幸的是,这不能写在单个开关中导致运行多个你应该移除break;,但这意味着第一个满足导致所有后续的执行无论条件如何......

说你必须转移到&#34; multi-if&#34;可能如下所示:

var myAge = parseInt(prompt("Enter your age:", "30"), 10),
    msg = [];

if (myAge < 0 && myAge > 10)
    msg.push("myAge is NOT between 0 and 10");
if (myAge >= 0 && myAge <= 10)
    msg.push("myAge is between 0 and 10");
if (myAge >= 80 || myAge <= 10)
    msg.push("myAge is 80 or above or 10 or below");
if (myAge >= 30 && myAge <= 39)
    msg.push("myAge is between 30 and 39");
if (myAge >= 80 && myAge <= 89)
    msg.push("myAge is between 80 and 89");

console.log(msg.join("\n"));

答案 3 :(得分:0)

您遗失了break和拼写错误mmyAge。并将true传递给switch

&#13;
&#13;
var myAge = parseInt(prompt("Enter your age:", "30"));
age(myAge);
function age(myAge){

    switch (true) {
        
        case (myAge >= 0 && myAge <=10):
            document.write("myAge is NOT between 0 and 10 <br />");
        break;
        case myAge >= 0 && myAge <= 10:
            document.write("myAge is between 0 and 10 ");
            break;

        case myAge >=80 && myAge <=10:
            document.write("myAge is 80 or above or 10 or below ");
            break;

        case myAge >= 30 && myAge <=39:
            document.write("myAge is between 30 and 39 ");              
            break;

        case myAge >= 80 && myAge <= 89:
            document.write("myAge is between 80 and 89 ");
            break;
        default:
              console.log(myAge);
        }
        
  }
&#13;
&#13;
&#13;