javascript中switch case跳转到错误大小写(如何正确使用break命令)

2022-01-19 00:00:00 switch-statement javascript

我的代码不是很长,所以我把它全部粘贴在这里.

My code is not so long so I am pasting all of it here.

代码不完整,但是当我运行它时,它首先跳转到它应该执行的 case start",然后跳转到 case end".我可以看到它,因为它会打印两个块的控制台日志文本.为什么要跳到结束"案例?

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>

推荐答案

问题是您的 case 代码后面缺少 break 关键字.没有中断,后续的块将被执行,这就是为什么 endstart 代码之后执行.您可以在此 W3Schools 链接上阅读更多相关信息.

The problem is that you are missing the break keyword after your case code. Without the break, subsequent blocks will be executed, that is why end is executed after start code. You can read more about this on this W3Schools link.

另外,来自 JS 参考:

与每个 case 标签关联的可选 break 语句确保一旦匹配的语句出现,程序就会跳出 switch在 switch 之后的语句处执行并继续执行.如果省略break,程序在下一个继续执行switch 语句中的语句.

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. If break is omitted, the program continues execution at the next statement in the switch statement.

所以你的代码应该是这样的:

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

您的结束"案例最后有一个返回,因此代码不会落入其他案例.理想情况下,每个结尾都应该有一个 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.

相关文章