如何让生成的数字一次显示?

时间:2014-08-22 14:42:20

标签: java arrays loops random joptionpane

import java.util.*;
import javax.swing.JOptionPane;
public class p1 
{
public static void main(String[] args) 
{
    int size = 50;

    ArrayList<Integer> list = new ArrayList<Integer>(size);
    for(int i = 1; i <= size; i++) 
    {
        list.add(i);
    }

    Random rand = new Random();
    while(list.size() > 0) 
    {
        int index = rand.nextInt(list.size());
        JOptionPane.showMessageDialog(null, "Selected: "+list.remove(index));
    }
}
}

当我运行此程序时,我生成的随机数都显示在单独的消息框中,如何更改代码以便在一个消息框中显示数组,即。你生成的数字是:1,4,5,6,33等。)另外,我如何更改代码,以便生成一定数量的数字,比如10,因为它打印出来的数量为50我可以告诉你。

3 个答案:

答案 0 :(得分:1)

除此之外:

JOptionPane.showMessageDialog(null, "Selected: "+list.remove(index));

将list.remove(index)放在字符串变量

然后在循环后放

  JOptionPane.showMessageDialog(null, "Selected: "+string variable);

你的错误只是将对话框放在循环中,除了它之外。

public static void main(String[] args) 
{
    int size = 10;

    ArrayList<Integer> list = new ArrayList<Integer>(size);
    for(int i = 1; i <= size; i++) 
    {
        list.add(i);
    }

    Random rand = new Random();
    String buffer = "";
    while(list.size() > 0) 
    {
        int index = rand.nextInt(list.size());
        buffer += ","+list.remove(index);

    }
    JOptionPane.showMessageDialog(null, "Selected: "+buffer);
}

编辑答案:必须制作两个变量大小和范围

public static void main(String[] args) 
{
    int size = 10;
    int range = 50;

    ArrayList<Integer> list = new ArrayList<Integer>(size);
    Random rand = new Random();
    for(int i = 1; i <= size; i++) 
    {
        list.add( rand.nextInt(range));
    }


    String buffer = "";
    while(list.size() > 0) 
    {
        int index = 0;

        if(list.size() != 1)
               buffer += list.remove(index)+",";
        else if(list.size() == 1)
             buffer += list.remove(index)+",";
        index++;
    }
    JOptionPane.showMessageDialog(null, "Selected: "+buffer);
}

答案 1 :(得分:0)

import java.util.*;
import javax.swing.JOptionPane;

public class test {
    public static void main(String[] args) {
        int size = 50;

        ArrayList<Integer> list = new ArrayList<Integer>(size);
        for (int i = 1; i <= size; i++) {
            list.add(i);
        }

        Random rand = new Random();

        StringBuilder sb = new StringBuilder();
        while (list.size() > 0) {
            int index = rand.nextInt(list.size());

            sb.append(list.remove(index) + ",");
            sb.deleteCharAt(sb.length() - 1);

        }

        JOptionPane.showMessageDialog(null, "Selected: " + sb.toString());
    }
}

答案 2 :(得分:0)

这是我相信的要求。

Random random = new Random();

StringBuffer buff = new StringBuffer();
for (int index = 1; index <= 50; index++) {
    buff.append(random.nextInt(50));
    if (index % 10 == 0) {
    buff.append('\n');
    } else {
    buff.append(',');
    }

}
buff.delete(buff.length() - 2, buff.length());

System.out.println(buff.toString());
JOptionPane.showMessageDialog(null, "Selected: " + buff.toString());