如何在java中使用for循环从数组列表打印部件
我是编程语言的新手 我有密码如何在java中使用for循环从数组列表打印部件,java,arrays,Java,Arrays,我是编程语言的新手 我有密码 import javax.swing.*; public class arr1{ public static void main(String args[]){ String str = JOptionPane.showInputDialog(null, "please choose a array : "); int n = Integer.parseInt(str); Integer list[] = {1, 3, 5, 7,
import javax.swing.*;
public class arr1{
public static void main(String args[]){
String str = JOptionPane.showInputDialog(null, "please choose a array : ");
int n = Integer.parseInt(str);
Integer list[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
for(n=0; n<=list.length; n++){
if (list[n]==0){
JOptionPane.showInputDialog(":", list[0]);
} else if (list[n]==1){
JOptionPane.showInputDialog(":", list[1]);
} else if (list[n]==2){
JOptionPane.showInputDialog(":", list[2]);
} else if (list[n]==3){
JOptionPane.showInputDialog(":", list[3]);
} else if (list[n]==4){
JOptionPane.showInputDialog(":", list[4]);
} else if (list[n]==5){
JOptionPane.showInputDialog(":", list[5]);
} else if (list[n]==6){
JOptionPane.showInputDialog(":", list[6]);
} else if (list[n]==7){
JOptionPane.showInputDialog(":", list[7]);
} else if (list[n]==8){
JOptionPane.showInputDialog(":", list[8]);
} else if (list[n]==9){
JOptionPane.showInputDialog(":", list[9]);
}
}
}
}
否则,如果我插入“n”输入为5,则输出应为
the output : 1, 3, 5
the output : 1, 3, 5, 7, 9
提前谢谢,希望任何人都能帮助我,不要投反对票,因为这真的是一个基本问题 只需循环直到
n
:
for (int i = 0; i < n; i++) {
JOptionPane.showInputDialog(":", list[i]);
}
for(int i=0;i
您应该这样打印它
for(int i =0; i < n; i++) {
//print list[i]
}
for(int i=0;i
如果您对Java不够熟悉,您应该从教程开始:从终端中的输入和输出开始,而不是从图形应用程序开始。虽然从技术上讲这不是一个答案,但我想指出,您将for循环中的n
变量设置回0,所以它总是在整个阵列中循环。@EInherjar yap!!我的逻辑水平真的很低,我是从回答部分得到的