小编laz*_*ANZ的帖子

数组JAVA中的非重复随机数

我想在一个数组中生成6个数字,同时对它进行比较,因此它不会相同或没有重复数字.例如,我想以任何顺序生成1-2-3-4-5-6,最重要的是不重复.所以我想的是逐个比较生成的数组中的当前数组,如果数字重复,它将重新运行该方法并再次随机化一个数字,这样就可以避免重复数字.

这是我的代码:

import javax.swing.*;
public class NonRepeat
{
    public static void main(String args[])
    {
        int Array[] = new int [6];
        int login = Integer.parseInt(JOptionPane.showInputDialog("ASD"));
        while(login != 0)
        {
            String output="";

            for(int index = 0; index<6; index++)
            {
                Array[index] = numGen();

                for(int loop = 0; loop <6 ; loop++)
                {
                    if(Array[index] == Array[loop])
                    {
                        Array[index] = numGen();
                    }
                }


            }

            for(int index = 0; index<6; index++)
            {
                output += Array[index] + " ";
            }


            JOptionPane.showMessageDialog(null, output);

        }



    }

    public static int …
Run Code Online (Sandbox Code Playgroud)

java arrays random

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

arrays ×1

java ×1

random ×1