這個問題在這里已經有了答案: javascript中帶標簽和不帶標簽有什么區別 6個答案 6 天前關閉。
此帖已于6 天前編輯并提交審核。
我有一個代碼
loop1:
for(j=0;j<players.x.length;j ){
//someting
loop2:
for(k=0;k<tankmain[players.type[j]].tankbottom.size.length;k ){
//someting else
loop3:
for(i=0;i<darts.x.length;i ){
if(hit){
//something
break loop1;
break loop2;
break loop3;
}
}}}
但無論我如何定位它,最后兩個中斷回圈都變灰了
uj5u.com熱心網友回復:
您將三個休息時間放在一起。但是當你在回圈中遇到第一個中斷時,你已經離開了回圈,因此其他兩個不會被呼叫。有關一種可能的解決方案,請參見下面的示例。
let loop1 = [1,2,3,4,5,6,7,8];
let loop2 = [10,11,12,13,14,15];
let loop3 = [50,51,52,53,54,55];
let breakLoop1 = false;
let breakLoop2 = false;
let hit = false;
for(j = 0; j < loop1.length; j ){
console.log(`${j} from loop1`);
//someting
for(k = 0; k < loop2.length; k ){
console.log(`${k} from loop2`);
//someting else
for(i = 0; i < loop3.length; i ){
console.log(`${i} from loop3`);
if(i == 2) hit = true;
if(hit) {
//something
breakLoop1 = true;
breakLoop2 = true;
console.log("leaving loop 3");
break; //break loop3
}
}
//break inside loop2
if(breakLoop2) {
console.log("leaving loop 2");
break;
}
}
//break inside loop1
if(breakLoop1) {
console.log("leaving loop 1");
break;
}
}
uj5u.com熱心網友回復:
嘗試使用標志變數方法,因為將標志變數設定為 False 是打破多個回圈的另一種方法。在退出內部回圈之前,可以將變數設定為 True。在內回圈之后,外回圈中必須有一個 if 塊。
示例代碼:
def elementInArray(arr, x):
flag = False # Defining the flag variable
# Iterating through all
# lists present in arr:
for i in arr:
# Iterating through all the elements
# of each of the nested lists in arr:
for j in i:
# Checking if any element in the
# nested list is equal to x
# and assigning True value to
# flag if the condition is met
# else printing the element j:
if j == x:
flag = True
print('Element found')
break
else:
print(j)
# If the inner loop terminates due to
# the break statement and flag is True
# then the following if block will
# be executed and the break statement in it
# will also terminate the outer loop. Else,
# the outer loop will continue:
if flag == True:
break
# Driver Code:
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
x = 4
elementInArray(arr, x)
uj5u.com熱心網友回復:
您可以使用標簽:
// Mark this loop, name it "outerLoop";
outerLoop: while (1) {
while(1) {
while(1) {
if (condition) {
// break out of the specific loop "outerLoop". Works with continue too
break outerLoop;
}
}
}
}
有關更多資訊,請參閱https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/505286.html
標籤:javascript 循环 for循环
上一篇:基于另一列R的字串值的計算欄位
