非重复阵列的JAVA内的随机数 [英] Non-repeating random numbers inside array JAVA

查看:164
本文介绍了非重复阵列的JAVA内的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成的阵列内,并在同一时间6个数字,它相比,所以它不会是相同的或不重复的数字具有。例如,我想生成任何顺序1-2-3-4-5-6,最重要的不重复。因此,我认为是一个比较阵列产生一个电流阵列,如果数量重复,它将重新运行的方法,并再次随机一些,因此将避免号码重复。

I would like to generate 6 numbers inside an array and at the same time, having it compared so it will not be the same or no repeating numbers. For example, I want to generate 1-2-3-4-5-6 in any order, and most importantly without repeating. So what I thought is to compare current array in generated array one by one and if the number repeats, it will re-run the method and randomize a number again so it will avoid repeating of numbers.

下面是我的code:

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 numGen()
    {
        int random = (int)(1+Math.random()*6);
        return random;
    }
}

我一直在想它2小时,仍然无法产生6个号码不重复。
希望我的问题将得到解答。

I've been thinking it for 2 hours and still cant generate 6 numbers without repeating. Hope my question will be answered.

BTW,即时通讯在codeS新的,所以请我只是想用比较它环或,而循环和如果别人

Btw, Im new in codes so please I just want to compare it using for loop or while loop and if else.

推荐答案

下面是解决方案根据您的code -

Here is the solution according to your code -

您只需要更改numGen方法 -

You just need to change the numGen method -

public static int numGen(int Array[])
{

    int random = (int)(1+Math.random()*6);

    for(int loop = 0; loop <Array.length ; loop++)
    {
        if(Array[loop] == random)
        {
            return numGen(Array);
        } 
    }


    return random;
}

完成code是 -

Complete code is -

import javax.swing.*;
public class NonRepeat
{
    public static void main(String args[])
    {

        int login = Integer.parseInt(JOptionPane.showInputDialog("ASD"));
        while(login != 0)
        {
            int Array[] = new int [6];
            String output="";

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


            }

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


            JOptionPane.showMessageDialog(null, output);

        }



    }

    public static int numGen(int Array[])
    {

        int random = (int)(1+Math.random()*6);

        for(int loop = 0; loop <Array.length ; loop++)
        {
            if(Array[loop] == random)
            {
                return numGen(Array);
            } 
        }


        return random;
    }
}

这篇关于非重复阵列的JAVA内的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆