Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript switch语句不工作,只有第一种情况触发操作_Javascript_Switch Statement - Fatal编程技术网

Javascript switch语句不工作,只有第一种情况触发操作

Javascript switch语句不工作,只有第一种情况触发操作,javascript,switch-statement,Javascript,Switch Statement,在昨天学习了switch语句之后,我对编写switch语句是个新手 出于某种原因,这是行不通的 checkCase(2); checkCase(1); checkCase(0); function checkCase(priorityType){ switch(priorityType){ case 2: print(priorityType); break; case 1: print(pr

在昨天学习了switch语句之后,我对编写switch语句是个新手

出于某种原因,这是行不通的

checkCase(2); checkCase(1); checkCase(0); function checkCase(priorityType){ switch(priorityType){ case 2: print(priorityType); break; case 1: print(priorityType); break; case 0: print(priorityType); break; } } 复选框(2); 复选框(1); checkCase(0); 功能检查用例(priorityType){ 开关(优先型){ 案例2: 打印(优先打印); 打破 案例1: 打印(优先打印); 打破 案例0: 打印(优先打印); 打破 } } “警报(2)”已触发,1和0未触发。 我已经将案例2:与案例1:颠倒过来,再次运行代码,2再次被触发,1没有。 我也尝试过添加break;并继续,;对案件,但仍然没有什么

为什么呢?我做错了什么? ----------------------编辑--------------------------- 很多人回答说我需要加上“break;”我现在对每一行都做了。 仍然没有产出。我还将“警报”改为“打印”。没有区别

-------------编辑2-----------------------
糟糕的是,“休息”现在开始起作用了。我不知道上次检查时发生了什么。可能需要重新启动ff。

您必须
中断在每个案例之后

function checkCase(priorityType){
    switch(priorityType){
        case 2:
             alert(priorityType);
        break;         

        case 1:
            alert(priorityType);    
        break;        

        case 0:
            alert(priorityType);
        break;

        //my two cents
        default:
            alert("No intended code for"+priorityType);
     }
}

checkCase(2);
checkCase(1);
checkCase(0);

你必须
break在每个案例之后

function checkCase(priorityType){
    switch(priorityType){
        case 2:
             alert(priorityType);
        break;         

        case 1:
            alert(priorityType);    
        break;        

        case 0:
            alert(priorityType);
        break;

        //my two cents
        default:
            alert("No intended code for"+priorityType);
     }
}

checkCase(2);
checkCase(1);
checkCase(0);

因为没有break语句,所以一旦找到匹配的案例,它和后续案例都将触发

所以当你这样做的时候:

checkCase(2);
checkCase(1);
checkCase(0);
您将获得:

alert(2);
alert(2);
alert(2);
alert(1);
alert(1);
alert(0);
当您这样做时:

checkCase(2);
checkCase(1);
checkCase(0);
您将获得:

alert(2);
alert(2);
alert(2);
alert(1);
alert(1);
alert(0);
当您这样做时:

checkCase(2);
checkCase(1);
checkCase(0);
您将获得:

alert(2);
alert(2);
alert(2);
alert(1);
alert(1);
alert(0);
如果您希望获得:

alert(2);
alert(1);
alert(0);
您需要将开关更改为包含
break语句

function checkCase(priorityType){ 
   switch(priorityType){
     case 2:
         alert(priorityType);
         break;           
     case 1:
        alert(priorityType);       
        break;     
     case 0:
        alert(priorityType);
        break;
     }
}

因为没有break语句,所以一旦找到匹配的案例,它和后续案例都将触发

所以当你这样做的时候:

checkCase(2);
checkCase(1);
checkCase(0);
您将获得:

alert(2);
alert(2);
alert(2);
alert(1);
alert(1);
alert(0);
当您这样做时:

checkCase(2);
checkCase(1);
checkCase(0);
您将获得:

alert(2);
alert(2);
alert(2);
alert(1);
alert(1);
alert(0);
当您这样做时:

checkCase(2);
checkCase(1);
checkCase(0);
您将获得:

alert(2);
alert(2);
alert(2);
alert(1);
alert(1);
alert(0);
如果您希望获得:

alert(2);
alert(1);
alert(0);
您需要将开关更改为包含
break语句

function checkCase(priorityType){ 
   switch(priorityType){
     case 2:
         alert(priorityType);
         break;           
     case 1:
        alert(priorityType);       
        break;     
     case 0:
        alert(priorityType);
        break;
     }
}

两个问题:使用
alert
进行调试有问题,您需要
break
语句

function checkCase(priorityType){ 
   switch(priorityType){
     case 2:
         alert(priorityType);
         break;           
     case 1:
        alert(priorityType);       
        break;     
     case 0:
        alert(priorityType);
        break;
     }
}
尝试添加
break
s并在中使用
print
,我想您会看到正确的结果

checkCase(2);
checkCase(1);
checkCase(0);
function checkCase(priorityType){
switch(priorityType){
     case 2:
         print(priorityType);               break;
        case 1:
        print(priorityType);            break;
        case 0:
        print(priorityType);   break;
     }
}

两个问题:使用
alert
进行调试有问题,您需要
break
语句

function checkCase(priorityType){ 
   switch(priorityType){
     case 2:
         alert(priorityType);
         break;           
     case 1:
        alert(priorityType);       
        break;     
     case 0:
        alert(priorityType);
        break;
     }
}
尝试添加
break
s并在中使用
print
,我想您会看到正确的结果

checkCase(2);
checkCase(1);
checkCase(0);
function checkCase(priorityType){
switch(priorityType){
     case 2:
         print(priorityType);               break;
        case 1:
        print(priorityType);            break;
        case 0:
        print(priorityType);   break;
     }
}

除了我把它改回了
alert()
之外,你的代码是什么。在中断后,您将看到3个警报。Firefox通常会缓存页面和数据,因此如果您更新代码,您可能需要清除缓存或重新启动浏览器才能看到更改。除了我把它改回了
alert()
之外,你的代码是什么。在中断后,您将看到3个警报。Firefox通常会缓存页面和数据,因此如果您更新代码,您可能需要清除缓存或重新启动浏览器才能看到更改。