开关&& for循环 [英] Switch && for loops

查看:100
本文介绍了开关&& for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有'for'循环

并且在他里面有一个'switch'循环。

但是开关循环只执行ONCE。

为什么?我怎么能让它可以执行多次我想要的?



例如:



for (让我= 0;我< 5; i ++){



开关(某事[i]){

案例1:

alert(案例1已被激活!);

休息;

案例2:

alert( 案例2已被激活!);

休息;

}



我是什么尝试过:



寻找类似的文章问题。

i have a 'for' loop
and inside of him an 'switch' loop.
but the switch loop executes just ONCE.
why? and how can i make it executable as many time as i want to?

example:

for(let i = 0; i < 5; i++){

switch(something[i]){
case 1:
alert("case 1 has been activated!");
break;
case 2:
alert("case 2 has been activated!");
break;
}

What I have tried:

look for similar articles problems.

推荐答案

A switch 语句不是循环。此外,如果的东西[i] 不是1或2,那么什么都不会被拯救,并且会默默地通过。

你将不得不在上为行设置断点并逐行调试以查看发生了什么。

您可能想要添加默认案例来说明意料之外的值:

A switch statement is not a loop. Moreover, if what is at something[i] is not 1 or 2, nothing will be excuted and will silently pass.
You will have to put a breakpoint on the for line and debug line by line to see what is going on.
You may want to add a default case to account for unanticipated values:
switch (something[i]) {
   // ...
   default:
      alert("unexpected value: " + something[i]);
      break;
}


Quote:

但是循环执行只是ONCE。

but the switch loop executes just ONCE.



可能是因为开关不是循环。你需要学习语言结构,我们不能为你服务。

JavaScript教程 [ ^ ]


这篇关于开关&amp;&amp; for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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