简体   繁体   English

切换大小写会在javascript中跳到错误的大小写(如何正确使用break命令)

[英]Switch case jumps to wrong case in javascript (how to properly use the break command)

My code is not so long so I am pasting all of it here. 我的代码不太长,所以我将所有代码粘贴到这里。

The code is not complete but when I run it it first jumps to case "start" which it is supposed to, and then jumps to case "end". 该代码不是完整的,但是当我运行它时,它首先跳转到应该假定的大小写“开始”,然后跳转到大小写“结束”。 I can see it because it prints both blocks' console log texts. 我可以看到它,因为它同时打印了两个块的控制台日志文本。 Why is it jumping to the "end" case? 为什么会跳到“尽头”的情况?

<html>
    <body>
        <script>
            function stepStream(stream,step){
                switch (stream[step]){
                    case "start":
                        console.log("Started reading stream...");
                    case "end":
                        var success = "Finished reading dataStream.";
                        console.log(success);
                        return success;
                    default:
                        throw "Data stream format is bad";                  
                    case "gesture":
                        console.log("Running case gesture! But why?");
                        step+=1;
                        stepStream(stream,step);
                    case "say":
                        step+=1;
                        stepStream(stream,step);
                    case "sleep":
                        step+=1;
                        stepStream(stream,step);
                }

            }

            var sentence1 = "Where are my bananas? I thought you put them in my bag?";
            var sentence2 = "This is a rather irritating situattion.";  
            var dataStream = ["start","gesture","banzai","sleep",1.0,"say",sentence1,
                                "say",sentence2,"gesture","kubikasige","end"];
            stepStream(dataStream,0);//Second parameter sets where to start reading the dataStream.


        </script>
    </body>
</html>

The problem is that you are missing the break keyword after your case code. 问题在于您在case代码之后缺少break关键字。 Without the break, subsequent blocks will be executed, that is why end is executed after start code. 没有中断,将执行后续块,这就是为什么在start代码之后执行end原因。 You can read more about this on this W3Schools link . 您可以在此W3Schools链接上阅读有关此内容的更多信息。

Additionally, from the JS reference : 另外,从JS参考资料中

The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. 与每个case标签关联的可选break语句可确保一旦执行了匹配的语句,程序便会退出switch,并在switch之后的语句处继续执行。 If break is omitted, the program continues execution at the next statement in the switch statement. 如果省略break,程序将在switch语句的下一个语句处继续执行。

So your code should look like: 因此,您的代码应如下所示:

function stepStream(stream,step){
                switch (stream[step]){
                    case "start":
                        console.log("Started reading stream...");
                        break;
                    case "end":
                        var success = "Finished reading dataStream.";
                        console.log(success);
                        return success;
                    default:
                        throw "Data stream format is bad";                  
                    case "gesture":
                        //commUGesture(stream[i+1]);
                        //createLogLine("robot:CommU","event:gesture:"+stream[i+1]);
                        console.log("Running case gesture! But why?");
                        step+=1;
                        stepStream(stream,step);
                        break;
                    case "say":
                        step+=1;
                        stepStream(stream,step);
                        break;
                    case "sleep":
                        step+=1;
                        stepStream(stream,step);
                        break;
                }

Your "end" case has a return at the end, hence the code doesn't fall through to the other cases. 您的“结束”案例在结尾处有一个返回值,因此代码不会影响其他案例。 Ideally, there should be a break at the end of each. 理想情况下,每个结尾都应有一个break时间。

您忘记在开始块中添加一个break语句,因此它一直延伸到结束块。

Problem is simple all switch case have to end with break or return statements in your case that is missing. 问题很简单,所有丢失的switch case都必须以breakreturn语句结尾。

switch(var1)
{
    case "start":
        console.log("Started");
        break;
    case "end":
         console.log("stopped");
         return "";
     .
     .
     .
}

该代码将在第一个匹配的“ case”开始运行,但是只有在到达“ break”或“ return”语句时才停止运行。

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

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