切换案例跳转到javascript中的错误案例(如何正确使用break命令)

时间:2018-02-18 21:58:48

标签: javascript switch-statement

我的代码不是很长,所以我在这里粘贴所有内容。

代码不完整但是当我运行它时,它首先跳到它应该的情况“start”,然后跳到case“end”。我可以看到它,因为它打印了两个块的控制台日志文本。为什么跳到“结束”的情况?

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

4 个答案:

答案 0 :(得分:2)

问题是您在break代码后缺少case关键字。如果没有中断,将执行后续块,这就是在end代码之后执行start的原因。您可以在this W3Schools link上了解详情。

此外,来自JS reference

  

与每个案例标签关联的可选break语句可确保   一旦匹配的语句,程序就会断开   执行并继续执行切换后的语句。如果   break被省略,程序继续执行下一步   switch语句中的语句。

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

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

答案 1 :(得分:0)

您忘记在起始块中添加break语句,因此它会一直到结束块。

答案 2 :(得分:0)

问题很简单所有switch case都必须以您的案例中的breakreturn语句结束。

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

答案 3 :(得分:0)

代码将在第一个匹配&#34; case&#34;开始运行,但它只在到达&#34; break&#34;时停止运行。或&#34;返回&#34;语句;