Java:从返回的数组中组织元素

时间:2014-05-05 03:54:04

标签: java arrays

有没有更好的方法来编写此代码?我尝试了一个for循环遍历每个元素,但随后它使我的JOptionPane显示5个窗口。另外,我为每个" getValueX"分配了一个int变量,但仍然有5个窗口显示每个元素。谢谢!

int v11, v12, v13, v14, v15;
int v21, v22, v23, v24, v25;

v11 = inputDie1[0].getValue1();
v12 = inputDie1[1].getValue1();
v13 = inputDie1[2].getValue1();
v14 = inputDie1[3].getValue1();
v15 = inputDie1[4].getValue1();

v21 = inputDie2[0].getValue2();
v22 = inputDie2[1].getValue2();
v23 = inputDie2[2].getValue2();
v24 = inputDie2[3].getValue2();
v25 = inputDie2[4].getValue2();

JOptionPane.showMessageDialog(null, v11 + "   " + v21 + "\n" + v12 + "   " + v22 + "\n" 
           + v13 + "   " + v23 + "\n" + v14 + "   " + v24 +  "\n" + v15 + "   " + v25);

1 个答案:

答案 0 :(得分:1)

您可能有5个对话框,因为您将对话框调用放在循环中。考虑这种方法:

StringBuilder message = new StringBuilder();
for (int i = 0; i < 5; i++) {
    message.append(inputDie1[i].getValue1() + "   " + inputDie2[i].getValue2() + "\n");
}

JOptionPane.showMessageDialog(null, message.toString());