Java数组: How do I make a lottery game?

标签 java arrays sorting

所以我需要制作一个可以制作彩票游戏的程序。

我的代码支持 4 种游戏,6-42、MegaLotto(6-45)、SuperLotto(6-49) 和 GrandLotto(6-55) 它会询问要玩哪个游戏,输入 1 表示 6-42,依此类推。

游戏中有 10 位玩家,每个玩家的乐透中都有不重复的随机数字。 (例如,玩家 1:1,2,3,4,5,6,玩家 2:2,3,4,5,6,7 等。)

这就是获胜组合 (例如获胜组合:1,2,3,4,5,6)

如何生成新号码来代替重复号码? (例如1,1,2,3,4,5,重复的数字将生成一个新的数字,因此,有7,1,2,3,4,5)

我的源代码:

public static void main(String args[])
{

        int c1[]= new int[10];
        int a1[]= new int[6];
        int a2[]= new int[6];
        int a3[]= new int[6];
        int a4[]= new int[6];
        int a5[]= new int[6];
        int a6[]= new int[6];
        int a7[]= new int[6];
        int a8[]= new int[6];
        int a9[]= new int[6];
        int a10[]= new int[6];
        int aMaster[]= new int[6];

        int a=0,b=0,c=0,d=0;
        int x,x1,x2,x3,x4;

        String help="";
        String holp="";
        char went='A';



        JDialog.setDefaultLookAndFeelDecorated(true);
        int numbers[]= new int[6];


    b= Integer.parseInt(JOptionPane.showInputDialog(null,"Type [1] for 6-42\nType [2] for Mega\nType [3] for Super\nType [4] for Grand\nType [5] Go away and never come back!","Play!!!", JOptionPane.WARNING_MESSAGE));

    while(b!=5)
    {
        switch(b)
            {

                case 1:

                    holp ="6-42 Lotto:\n";


                    for(x=0; x<a.length; x++)
                    {
                        a1[x]= AllGen(1,42);
                    }
                break;
            }
    }


}

AllGen 是一个数字生成器方法,例如 (1,42) 将生成 1 到 42 之间的随机数。

最佳答案

您可以使用 SET 。这是一个不允许重复数字的集合。您可以继续添加数字,直到列表达到您需要的长度。这样,每次重复一个数字时,循环就会多进行一次。例如,如果您需要 6 个介于 0 - 50 之间的中奖号码,您可以使用如下内容:

Set winningNums = new TreeSet();
Random rand = new Random();

while (winningNums.size() < 6) {
    winningNums.add(rand.nextInt(51));
}

System.out.println(winningNums.toString());

您需要将其集成到您的程序中,但这将向您展示如何生成不重复的数字列表。我使用了 TreeSet 来对列表进行排序,但如果您不需要,可以使用其他的。

示例输出

[9, 10, 12, 24, 31, 37]
[1, 10, 11, 19, 43, 45]
[6, 20, 21, 33, 40, 48]

如果 0 不是允许的数字,您可以在每次迭代时将 1 添加到随机生成的 int 中。

关于Java数组: How do I make a lottery game?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21324959/

相关文章:

java - 错误膨胀类 AppCompatButton

python - 从哪里开始 : Python script that lets a user rank elements against each other by comparison

c - 我对这个函数做错了什么,它没有结束

java - 字符串加密有效,byte[]数组类型加密无效

java - 如何使用java在资源组中创建IoTHub资源?

Java 流 : Collect a nested collection

javascript - 如何使用 document.cookie 来存储和检索数组中的值?

python - 包含 numpy 范围内的数字

c - 如何打印数组中元素的数量?

ios - 快速搜索和排序