Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 简单开关语句_Javascript_Switch Statement - Fatal编程技术网

Javascript 简单开关语句

Javascript 简单开关语句,javascript,switch-statement,Javascript,Switch Statement,我试图用javascript编写一个简单的switch语句,询问用户的输入速度。令我沮丧的是,当这段代码执行最后一个警报时,我得到的总是默认情况。请告诉我我做错了什么 根据我的经验,开关箱中不能有操作员;你必须有一个确定的值。在这种情况下,即使开关看起来更好,也应该使用IF-ELSE块 编辑:我还找到了一个类似问题的答案。在比较提示响应之前,需要将其转换为整数,并且需要将开关更改为一组IFs var speed = prompt("Do you know how to type?"); spee

我试图用javascript编写一个简单的switch语句,询问用户的输入速度。令我沮丧的是,当这段代码执行最后一个警报时,我得到的总是默认情况。请告诉我我做错了什么

根据我的经验,开关箱中不能有操作员;你必须有一个确定的值。在这种情况下,即使开关看起来更好,也应该使用IF-ELSE块


编辑:我还找到了一个类似问题的答案。

在比较提示响应之前,需要将其转换为整数,并且需要将开关更改为一组IFs

var speed = prompt("Do you know how to type?");
speed = speed.toLowerCase();
if (speed === "yes" ) {
    var howFast = prompt("what is your wpm?(the answer must be a number between 1 and 200");
    switch(howFast) {
        case (howFast <= 10):
        console.log("you are a snail! practice and type at least 20 wpm, then try this again.");
        break;
        case (howFast <= 30):
        console.log("you are still pretty slow, but you're getting there!");
        break;
        case (howFast <= 50):
        console.log("you are getting there, keep trying");
        break;
        case (howFast <= 90):
        console.log("WoW! Excellent job! Your tenacity has paid off");
        break;
        case (howFast > 90):
        console.log("you are a megaracer! congratulations!");
        break;
        default:
        console.log("DOES NOT COMPUTE... You're either superfast or playing around!");



        }

    } else { alert("learn how to type and comeback.");}
只要改变一下:

<script>
    var speed = prompt("Do you know how to type?");
    var howFast;
    var logMessage;

    speed = speed.toLowerCase();

    if (speed === "yes" ) {
        howFast = parseInt(prompt("what is your wpm?..."));
        logMessage = "DOES NOT COMPUTE... You're either superfast or playing around!";

        if (howFast <= 10)
            logMessage = "you are a snail! practice and type at least 20 wpm, then try this again.";

        if (howFast <= 30)
            logMessage = "you are still pretty slow, but you're getting there!";

        if (howFast <= 50)
            logMessage = "you are getting there, keep trying";

        if (howFast <= 90)
            logMessage = "WoW! Excellent job! Your tenacity has paid off";

        if (howFast > 90)
            logMessage = "you are a megaracer! congratulations!";

        console.log(logMessage);

    } else {

        alert("learn how to type and comeback.");

    }
</script>

它应该会起作用。
演示::

这有点像黑客,但您可以在JS case语句中使用如下表达式:

switch(true) {
..
我让这个工作:

var wpm = parseInt(howFast); // convert string to number first

switch(true)
{
    case wpm >= 0 && wpm <= 10:
    console.log('snail');
    break;

    case wpm > 10 && wpm <= 30:
    console.log('slowpoke');
    break;

    // etc.
}
jsFiddle:


希望对您有所帮助

在这种情况下,没有适用于每种情况的表达式,因此,使用if-else块代替switch更有意义。

尝试switchparseInthowFast
var wpm = parseInt(howFast); // convert string to number first

switch(true)
{
    case wpm >= 0 && wpm <= 10:
    console.log('snail');
    break;

    case wpm > 10 && wpm <= 30:
    console.log('slowpoke');
    break;

    // etc.
}
var speed = prompt("Do you know how to type?");
speed = speed.toLowerCase();
if (speed === "yes" ) {
    var howFast = prompt("what is your wpm?(the answer must be a number between 1 and 200");
    switch(true) {
        case (parseInt(howFast) <= 10):
        console.log("you are a snail! practice and type at least 20 wpm, then try this again.");
        break;
        case (parseInt(howFast) <= 30):
        console.log("you are still pretty slow, but you're getting there!");
        break;
        case (parseInt(howFast) <= 50):
        console.log("you are getting there, keep trying");
        break;
        case (parseInt(howFast) <= 90):
        console.log("WoW! Excellent job! Your tenacity has paid off");
        break;
        case (parseInt(howFast) > 90):
        console.log("you are a megaracer! congratulations!");
        break;
        default:
            console.log("DOES NOT COMPUTE... You're either superfast or playing around!");  
    }
    } else { alert("learn how to type and comeback.");}