Java 简单彩票程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28321760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:00:03  来源:igfitidea点击:

Java Simple Lottery Program

javaarrays

提问by berkc

I tried to create a simple lottery program. Here is a problem: it still prints same numbers. For example I got 33 21 8 29 21 10as output. Everytime when random number is generated, code checks if that number is already generated, then it creates a new random number but after that it doesn't check again. I couldn't find a way to do that.

我试图创建一个简单的彩票程序。这是一个问题:它仍然打印相同的数字。例如我得到了33 21 8 29 21 10作为输出。每次生成随机数时,代码都会检查该数字是否已经生成,然后它会创建一个新的随机数,但之后不会再次检查。我找不到办法做到这一点。

public static void main(String[] args)
{

    int[] lottery = new int[6];
    int randomNum;

    for (int i = 0; i < 6; i++)
    {
        randomNum = (int) (Math.random() * 50); //Random number created here.
        for (int x = 0; x < i; x++)
        {
            if (lottery[i] == randomNum) // Here, code checks if same random number generated before.
            {
                randomNum = (int) (Math.random() * 50);//If random number is same, another number generated.
            }

        }
        lottery[i] = randomNum;
    }

    for (int i = 0; i < lottery.length; i++)
        System.out.print(lottery[i] + " ");

}

采纳答案by Florent Bayle

There are 2 problems with your code:

您的代码有两个问题:

  • you check if lottery[i]and randomNumare the same, it should be lottery[x]
  • when you re-generate a random number, you don't check it against the first numbers in lottery.
  • 你检查lottery[i]randomNum是否相同,应该是lottery[x]
  • 当您重新生成一个随机数时,您不会根据lottery.

Here is a corrected version:

这是一个更正的版本:

public static void main(String[] args) {

    int[] lottery = new int[6];
    int randomNum;

    for (int i = 0; i < 6; i++) {
        randomNum = (int) (Math.random() * 50); // Random number created here.
        for (int x = 0; x < i; x++) {
            if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
            {
                randomNum = (int) (Math.random() * 50);// If random number is same, another number generated.
                x = -1; // restart the loop
            }

        }
        lottery[i] = randomNum;
    }

    for (int i = 0; i < lottery.length; i++)
        System.out.print(lottery[i] + " ");

}

回答by Peter Lawrey

You are changing the random number while you are checking it. You need to pick one random number and check whether it is present or not.

您在检查随机数时正在更改随机数。您需要选择一个随机数并检查它是否存在。

BTW A shorter approach is to use a shuffle.

顺便说一句,更短的方法是使用随机播放。

// give me all the number 1 to 50
List<Integer> list = IntStream.range(1, 51).boxed().collect(Collectors.toList());
// shuffle them.
Collections.shuffle(list);
// give me the first 6
System.out.println(list.subList(0, 6));

回答by marcS

A simple solution, between the first (who could be very abstract for a not Java programmer) and the 2nd (not assuring the unicity of the number list).

一个简单的解决方案,介于第一个(对于非 Java 程序员来说可能非常抽象)和第二个(不保证数字列表的唯一性)之间。

    Collection<Integer> liste = new ArrayList<Integer>();
    for (int i = 0; i < 6; i++)
    {

        Boolean ap = false; 
        while (!ap)
        {
            Integer randomNumber  =  (int) (Math.random() * 50);

            if (! liste.contains(randomNumber)){

                liste.add(randomNumber);
                ap = true;
            }
        }
    }


    for (Integer liste1 : liste) {
        System.out.print(liste1+" ");

    }

回答by James Wright

try this one, it creates 12 x (6 out of 45)

试试这个,它创建了 12 x(45 个中的 6 个)

public static void main(String[] args) {
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < 12; i++){
        Integer[] tipp = new Integer[6];
        int n = 0;
        do {
            int r = random.nextInt(45) + 1;
            if (Arrays.asList(tipp).indexOf(r)<0){
                tipp[n]= r;
                n++;
            }
        } while (n<=5);
        Arrays.sort(tipp);
        System.out.println(Arrays.toString(tipp));
        }
}

回答by ric

public static void main(String[] arg) {
    int[] lottery = new int[6];
    int randomNum;
    c1:
        for (int i = 0; i < 6; i++) {
            randomNum = (int) (Math.random() * 50); // Random number created here.
            if(randomNum == 0) {
                continue c1;
            }   
            for (int x = 0; x < i; x++) {
                if (lottery[x] == randomNum ) // Here, code checks if same random number generated before.
                {
                    randomNum = (int) (Math.random() * 50);// If random number is same, another number generated.
                    x = -1; // restart the loop
                }

            }
            lottery[i] = randomNum;
        }

    for (int i = 0; i < lottery.length; i++)
        System.out.print(lottery[i] + " ");

}