如何在多个输入的一个消息对话框中显示整个数组?该代码应该询问用户他们想要输入多少个数字,让用户输入这些数字,然后以相反的顺序显示它们。一切似乎都在工作,除了输出为数组中的每个数字显示一个单独的消息框,我希望它们都在一个空格或逗号之间。建议?
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog
(null, "How many numbers do you want to enter?");
int count = Integer.parseInt(input);
double[] myList = new double[count];
for(int a = 0; a < count; a++)
{
String numberInput = JOptionPane.showInputDialog
(null, "Enter a number: ");
int number = Integer.parseInt(numberInput);
myList[a] = number;
}
double[] secondList = reverse(myList);
printArray(secondList);
}
public static double[] reverse(double[] list)
{
double[] result = new double[list.length];
for (int i = 0, j = result.length - 1;
i < list.length; i++, j--)
{
result[j] = list[i];
}
return result;
}
public static void printArray(double[] array)
{
for (int a = 0; a < array.length; a++)
{
JOptionPane.showMessageDialog(null, array[a] + " ");
}
}
答案 0 :(得分:0)
JOptionPane.showMessageDialog(null, array[a] + " ");
处于循环中,所以它会。您应该在for循环中构造输出消息,并使用要显示的消息调用JOptionPane.showMessageDialog
一次。
public static void printArray(double[] array) {
StringBuilder builder = new StringBuilder();
for (int a = 0; a < array.length; a++) {
builder.append(array[a]).append(" ");
}
JOptionPane.showMessageDialog(null, builder.toString());
}
这样的事情应该有用。已经有一段时间了,因为我已经做了Java所以道歉,如果它有点偏离。