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

2023-12-02

我的代码不太长,所以我将其全部粘贴到这里。

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

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

问题是你错过了break您之后的关键字case代码。如果没有break,后续的块将会被执行,这就是为什么end之后执行start代码。您可以阅读有关此内容的更多信息这个 W3Schools 链接.

此外,从JS参考:

与每个 case 标签关联的可选的break语句确保 一旦匹配的语句出现,程序就会跳出 switch 执行并在 switch 之后的语句处继续执行。如果 省略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在每个的末尾。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

javascript中switch case跳转到错误的case(如何正确使用break命令) 的相关文章

随机推荐