使用Switch语句的JavaScript - 未获得预期的输出

时间:2014-10-05 22:15:07

标签: javascript html

我对此非常陌生并且需要学习很多东西。我在这段JavaScript中使用了Switch语句,但是我没有按照document.write行获得预期的输出。任何帮助,意见和建议表示赞赏。 - 谢谢!

            <script type="text/javascript">

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

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

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

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

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

                default:
                    document.write("You did not enter a number.  Please enter a number.");
                    break;
            }       

            document.write("<BR>Execution continues here");


        </script>

这就是我使用&#39; if&#39;。

写的内容
        <script type="text/javascript">

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

        if (myAge  >= 0 && myAge <= 10)
        {
            document.write ("myAge is between 0 and 10 <br />");
        }

        if (!(myAge >= 0 && myAge <=10))
        {
            document.write ("myAge is NOT between 0 and 10 <br />");
        }

        if (myAge >= 80 || myAge <= 10)
        {
            document.write ("myAge is 80 or above OR 10 or below <br />");
        }

        if (myAge >= 30 && myAge <=39 || (myAge >= 80 && myAge <= 89))
        {
            document.write("myAge is between 30 and 39 or myAge is between 80 and 89");
        }   


    </script>

这是使用&#39; switch&#39;的示例代码段。我必须参考。

<script type="text/javascript">

var secretNumber = prompt("Pick a number between 1 and 5:", "");
secretNumber = parseInt(secretNumber);

switch (secretNumber)
{
case 1:
   document.write("Too low!");
   break;

case 2:
   document.write("Too low!");
   break;

case 3:
   document.write("You guessed the secret number!");
   break;

case 4:
   document.write("Too high!");
   break;

case 5:
   document.write("Too high!");
   break;

default:
   document.write("You did not enter a number between 1 and 5.");
   break;
}
document.write("<BR>Execution continues here");

</script>

3 个答案:

答案 0 :(得分:3)

您需要重新考虑案例表达式中的逻辑。

让我在您的代码中进行替换,以说明错误

var test = myAge >= 0 && myAge <= 10;
if (test) {
   ...
} else if (!test) {
   ...
} else {
   // WILL NEVER BE RUN
}

如果您想使用switch语句,您必须了解您正在使用的数据背后的类型。

true : Boolean
false : Boolean
9 : Number
1.0 : Number

在下面的示例中, v必须与value1value2具有相同的类型。

switch(v)
{
   case (value1):
   ...       
   case (value2):
   ...
}

问题是以下类型在您的示例中不匹配。

v = Number
value1 = Boolean
value2 = Boolean

答案 1 :(得分:1)

对于这样的条件,请使用if

if (myAge  >= 0 && myAge <= 10){
    document.write("myAge is between 0 and 10");

}else if (!(myAge >= 0 && myAge <=10)){
    //...
}

switch用于多个确切的值,这不是您的情况:

switch(myAge){
    case 0:
        document.write("myAge is *exactly* 0");
        break;
    //...
}

答案 2 :(得分:1)

您可以创建一个仅评估真实语句的开关,填充查找第一个真实语句(case)(如果有),或者它将显示默认值。它将使用条件逻辑来工作。因此,您的代码只需添加true而不是myAge

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

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

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

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

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

    default:
        document.write("You did not enter a number.  Please enter a number.");
        break;
}

请注意,使用这样的开关是一种性能过度杀伤。使用if / else语句会更好/更快。

您可以在此处阅读有关切换Switch的更多信息,在底部您可以看到使用此方法(方法二)。