gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:17:24 25 4
gpt4 key购买 nike

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

我的代码支持 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com