简体   繁体   English

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

[英]JavaScript using the Switch Statement - Not getting the expected output

I'm quite new at this and have a lot to learn. 我对此很陌生,有很多东西要学。 I'm using the Switch statement in this piece of JavaScript but I'm not getting the expected output as per the document.write lines. 我在这段JavaScript中使用了Switch语句,但没有按照document.write行获得预期的输出。 Any help, comments and suggestions are appreciated. 任何帮助,意见和建议表示赞赏。 - Thank you! - 谢谢!

            <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>

This is what I wrote just using the 'if'. 这就是我仅使用“ if”编写的内容。

        <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>

This is the sample piece of code using 'switch' that I have to refer to. 这是我必须参考的使用“ switch”的示例代码。

<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>

You need to rethink the logic in the case expression. 您需要重新思考案例表达式中的逻辑。

Let me make a substitution in your code, to illustrate the error 让我在您的代码中进行替换,以说明错误

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

If you want to use a switch statement you have to understand the types behind the data you're using. 如果要使用switch语句,则必须了解正在使用的数据背后的类型。

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

In the example below, v must have the same type as value1 and value2 . 在以下示例中, v必须具有与value1value2相同的类型。

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

The issue is the following types don't match in your example. 问题是以下类型与您的示例不匹配。

v = Number
value1 = Boolean
value2 = Boolean

for conditions like this, use if : 对于这样的条件,请if以下情况下使用:

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

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

switch is for multiple exactly values, that isn't your situation: switch用于多个确切值,这不是您的情况:

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

You can make a switch that will evaluate only the true statements, it fill find the first true statement (case) if any, or it will show the default. 您可以进行一个开关,该开关将仅评估true语句,填充查找第一个true语句(大小写)(如果有),或者显示默认值。 It will use conditional logic to work. 它将使用条件逻辑来工作。 So your code to work just add true instead of myAge 因此,您要工作的代码只需添加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;
}

Just a note that using switch like this is a performance overkill. 请注意,使用这样的开关是性能过高的。 It is better/faster to use if / else statements. 最好/更快地使用if / else语句。

You can read more about switch here Switch and at the bottom you can see this method used (Method two). 你可以阅读更多关于开关位置切换 ,并在底部你可以看到使用这种方法(方法二)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM