简体   繁体   English

如何突围

[英]How to break out of loop

I'm trying to assign the value of optiontext to just one array value at a time. 我正在尝试一次将optiontext的值分配给一个数组值。

Is there a way I can break out of the code, because different values are to be assigned to different numbers. 有没有一种方法可以突破代码,因为将不同的值分配给了不同的数字。

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

Output: 输出:

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           

I just want quit for number one, not the whole 20 options and should only display the numbers that have values. 我只想退出第一名,而不是全部20个选项,而应该只显示具有值的数字。

what it should look like 它应该是什么样

     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

If you want to assign a value to just one array element then there is no need to use the loop. 如果您只想给一个数组元素分配一个值,则无需使用循环。 Eg if you want to assign optionText to first element then you can simply do: 例如,如果您想将optionText分配给第一个元素,则只需执行以下操作:

option[0] = optionText;

The i in option[i] specifies which array index you want. i in option[i]指定所需的数组索引。 If you just want quit for the first option in the array, you do not need a for loop. 如果只想退出数组中的第一个选项,则不需要for循环。

For example you should use, option[0] = optionText; 例如,您应该使用option[0] = optionText; hence, the first element is 0. 因此,第一个元素为0。

In this situation you does not need the for,, just create an integer pointer to fill the array.. something like this. 在这种情况下,您不需要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