2013-04-04 100 views
0

好吧,所以我遇到了以下循环的问题。这个循环的预期目标是userinput [k]是在方法中已经按字母顺序排序的名称列表。这些名称将在下面的InputDialog中显示,其中将输入表示度数的数字。JOptionPane输入为两个增量循环

我试图匹配名称和程度了。例如:在循环中,第一个用户输入将是userinput [0]。我想输入数字,然后蜜蜂程度[0],然后依此类推......

问题是我如何用J增量器存储输入。所以我基本上得到的错误是字符串度[] =整数......

for (int k = 0; k < userinput.length; k++){  
    for (int j = 0; j < userinput.length; j++) {      
     String input = JOptionPane.showInputDialog("Please enter the highest earned degree for the following person : " + userinput [ k ] + "\n 1 = BS \n 2 = MS \n 3 = PhD"); 
     String degree[] = Integer.parseInt(input[]); 
    } 
} 
+0

为什么你需要嵌套循环?这让你问同一个人多个('userinput.length')次。 – 2013-04-04 21:57:33

回答

1

这是你正在尝试做什么?

int[] degree = new int[userInput.length]; 
for(int k = 0; k < userInput.length; k++) { 
    String input = JOptionPane.showInputDialog("Please enter the highest earned degree for the following person : " + userinput [ k ] + "\n 1 = BS \n 2 = MS \n 3 = PhD"); 
    degree[k] = Integer.parseInt(input); 
} 
+0

这正是我想要的。谢谢您的帮助!我正在想办法解决这个问题。 – Chris 2013-04-04 22:03:46

+0

@Chris:不客气。无论何时想要遍历数组,只要使用一个'for'循环,除非它是多维的。否则,每个名称将在输入对话框中出现多次。 – 2013-04-04 22:07:48

1

这是你正在尝试做什么?

假设你已经声明userinput作为 一定大小的字符串数组,并分配其值,并宣称程度相同大小的字符串数组 。

for (int k = 0; k < userinput.length; k++){  
     String input = JOptionPane.showInputDialog("Please enter the highest earned degree for the following person : " + userinput [ k ] + "\n 1 = BS \n 2 = MS \n 3 = PhD"); 
     degree[k] = input; 
    } 
}