int[] red=new int[7]
int blue=0
for(int i=0i<7i=i+1){
red[i]=getRandom(33)
//判断重复
while(true){
boolean needcheck=false
for(int k=0k<ik=k+1) { if(red[i]==red[k]){ needcheck=true}}
if(needcheck){red[i]=red[i]+1if(red[i]==34){red[i]=1}}else{break}
}
//判断重复结束
}
//红球赋值结束
blue=getRandom(16)
System.out.println("生成的红球为:"+ red[0]+red[1]+red[2]+red[3]+red[4]+red[5]+red[6])
System.out.println("生成的蓝球为:"+ )
}
public int getRandom(int Max){
return Math.floor(Math.random()*33)+1
}
就是从36个数中选出7个,这36个数是什么可以自己指定,但必须保证不重复,我写的一个代码public
class
test
{
public
static
void
main(String[]
args)
{
int[]
a
=
new
int[36]
for(int
i
=
0
i
<
36
++i)
a[i]
=
i+1
for(int
i
=
0
i
<
5
++i)
randomSelect(a,
7)
}
static
void
randomSelect(int[]
a,
int
n)
{
randomShuffle(a,
7)
for(int
i
=
0
i
<
n
++i)
System.out.print(a[i]
+
"
")
System.out.println()
}
static
void
randomShuffle(int[]
a,
int
n)
{
for(int
i
=
0
i
<
n
++i)
{
int
r
=
(int)(Math.random()
*
a.length)
int
tmp
=
a[i]
a[i]
=
a[r]
a[r]
=
tmp
}
}
}
public class RandomDemo {public static void main(String[] args) {
LotteryTicket lt = new LotteryTicket()
int red[] = lt.getRed()
System.out.print("随机生成的红球为:")
for(int i=0i<6i++){
System.out.print(" "+red[i])
}
System.out.print("\t蓝球为:"+lt.getBlue())
}
}
class LotteryTicket{彩票生成类
private int[] red = new int[6]
public int[] getRed(){//返回红球
int i =0
for(i<6i++){
red[i] = (int)(Math.random()*33) +1
for(int j=0j<ij++){//保证了重复,我个从认为这个彩票好像不能重复吧
if(red[i]==red[j]){
red[i] = (int)(Math.random()*33) +1
j=0
}
}
}
return red
}
public int getBlue(){//返回蓝球
return (int)(Math.random()*16) +1
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)