簡體   English   中英

如何突圍

[英]How to break out of loop

我正在嘗試一次將optiontext的值分配給一個數組值。

有沒有一種方法可以突破代碼,因為將不同的值分配給了不同的數字。

for (int i = 1; i <=20; i++) 
    {
        option[i]=optionText;
    }

輸出:

1. Quit
2. Quit
3. Quit
4. Quit
5. Quit
6. Quit
7. Quit
8. Quit
9. Quit
10. Quit
11. Quit
12. Quit
13. Quit
14. Quit
15. Quit
16. Quit
17. Quit
18. Quit
19. Quit
20. Quit           

我只想退出第一名,而不是全部20個選項,而應該只顯示具有值的數字。

它應該是什么樣

     1. Quit
     2. Get information
     3. Display n integers, first descending then ascending
     4. Display n squares with odds descending, evens ascending
     5. Check if one string is the reverse of another

如果您只想給一個數組元素分配一個值,則無需使用循環。 例如,如果您想將optionText分配給第一個元素,則只需執行以下操作:

option[0] = optionText;

i in option[i]指定所需的數組索引。 如果只想退出數組中的第一個選項,則不需要for循環。

例如,您應該使用option[0] = optionText; 因此,第一個元素為0。

在這種情況下,您不需要for,只需創建一個整數指針來填充數組即可。

private int pointer =0;  // define it as an attribute (global variable)

protected void buton_click(){
    option[pointer]=optionText;
    pointer++;  // to be ready for the next index
}

暫無
暫無

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

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