繁体   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