簡體   English   中英

是否可以讓while循環“繼續循環直到不滿足此條件,然后再執行一次”?

[英]Is it possible to tell a while loop to 'keep looping until this condition is not met, then execute one more time'?

這是我的循環。

if (strSplit[i] == "(") {
    while (strSplit[i] != ")") {
        newElement = newElement.concat(strSplit[i]);
        i++;
    }
    /*add the closing bracket*/
    newElement = newElement.concat(strSplit[i]);
    i++;
}

因此,我們要做的是將數組中的元素連接在一起,使其既包含左括號又包含左括號。

我覺得有點笨拙,我不得不再重復一次連接以添加結束括號。

是否可以讓while循環“繼續循環直到不滿足此條件,然后再執行一次”?

do...while使用do...while

do{
    newElement = newElement.concat(strSplit[i]); i++;
}while(strSplit[i] != ")")

您可以只保存“(”和“)”的索引(我們叫索引ab ),然后將子字符串從a改為b+1

如果您喜歡冒險,可以對“正則表達式”進行一些研究,找到一種更優雅的方法:)

或者,為了“強力地”解決問題,請稍等一會兒,然后再添加一行代碼,就像循環中的內容一樣。

var count = 10;
var grow = '[';

while (count > 1)
  {
   grow = grow + count;
   count = count - 1;
   }

grow = grow + ']';

$('#putmehere').html(grow);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM