Java 如何打破循环
我试图一次只将Java 如何打破循环,java,loops,for-loop,Java,Loops,For Loop,我试图一次只将optiontext的值分配给一个数组值 有没有一种方法我可以打破代码,因为不同的值被分配给不同的数字 for (int i = 1; i <=20; i++) { option[i]=optionText; } 我只想退出第一个选项,而不是全部20个选项,应该只显示有值的数字 它应该是什么样子 1. Quit 2. Get information 3. Display n integers, first de
optiontext
的值分配给一个数组值
有没有一种方法我可以打破代码,因为不同的值被分配给不同的数字
for (int i = 1; i <=20; i++)
{
option[i]=optionText;
}
我只想退出第一个选项,而不是全部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]
中的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
}
那你为什么要分配20次呢?你对2-20的期望是什么?你理解你的代码吗?@ElliottFrischoptionText@Matthew这是一根绳子array@MrSimpleMind我知道它分配了20次,但我需要将optionText值分配给每个pne数字,因为每个数字的每个optionText值都不同
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
}