Java random numbers with no duplicates for a lottery using methods and arrays [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-23 05:49:05

问题


For my computer science class we're supposed to redo a lottery code we have already created, but now using methods and arrays. This is what I've done so far, but I can't figure out for the life of me why it is still duplicating the numbers and I can't find anything online that is close enough to what we're supposed to be doing/topics we have gone over. I am very new to this.

    public static void generaterand (int [] r)
    {

    Random  randomgen = new Random();

    boolean done;
    int i = 0;
    int j = 0;

    done = false;

    while (done == false)
    {
        for (i=0; i<6; i++)
        {
            r[i] = randomgen.nextInt(54)+1;

                for (j=0; j<6; j++)
                {   
                    if (r[i] != r[j])
                    {
                        done = true;
                    }
                }
        }
    }
    System.out.printf ("Lottery Numbers: %d %d %d %d %d %d\n", r[0], r[1], r[2], r[3], r[4], r[5]); 
}

回答1:


An efficient method in Java is to use a hashtree datatype, since it cannot store duplicates.



来源:https://stackoverflow.com/questions/53623086/java-random-numbers-with-no-duplicates-for-a-lottery-using-methods-and-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!