切换大小写会在javascript中跳到错误的大小写(如何正确使用break命令) [英] Switch case jumps to wrong case in javascript (how to properly use the break command)

查看:48
本文介绍了切换大小写会在javascript中跳到错误的大小写(如何正确使用break命令)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码不太长,所以我将所有代码粘贴到这里.

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>

推荐答案

问题是您在 case 代码后缺少了 break 关键字.没有中断,将执行后续块,这就是为什么在 start 代码之后执行 end 的原因.您可以在此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参考:

与每个案例标签关联的可选break语句可确保一旦匹配的语句是已执行,并在切换后的语句处继续执行.如果省略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.

这篇关于切换大小写会在javascript中跳到错误的大小写(如何正确使用break命令)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆