彩票号码生成程序代码

80酷酷网    80kuku.com

  程序import java.util.*;
import java.io.*;
import java.lang.*;

public class GetLotteryNumber{
public static int[] LotteryNumber=new int[7];
public static int temp=0;
static int temp1=0;
public static void main(String[] args){
if(args.length==0){
System.out.print("彩票号码为:");
OutputLotteryNumber();
}else{
try{
temp1=Integer.parseInt(args[0]);
}catch(Exception e){
System.out.println("ERROR:"+e.getMessage());
}
if(temp1>99999){temp1=999999;System.out.println("请注意!每次最多只能产生999999组彩票号码。");}

for(int i=0;i<temp1;i++){
System.out.print("第"+(int)(i+1)+"组彩票号码为: ");
OutputLotteryNumber();
System.out.println();
}
}
}

public static void OutputLotteryNumber(){
LotteryNumberTheard[] Lottery=null;
Random random;
long tempTime;
for(int j=0;j<LotteryNumber.length;j++){
try{
Lottery[j]=new LotteryNumberTheard(j);
random=new Random(java.lang.System.currentTimeMillis()%197807);
tempTime=random.nextInt((int)(java.lang.System.currentTimeMillis()%793));
Lottery[j].sleep(tempTime);
}catch(Exception e){
}
}

for(int j=0;j<LotteryNumber.length-1;j++)//排序
for(int i=j+1;i<LotteryNumber.length;i++)
if(LotteryNumber[j]>LotteryNumber[i]){
temp=LotteryNumber[j];
LotteryNumber[j]=LotteryNumber[i];
LotteryNumber[i]=temp;
}

/**
System.out.print("未整理前的号码:");
for(int j=0;j<LotteryNumber.length;j++)
System.out.print(LotteryNumber[j]+" ");
System.out.println();
**/

//排除重复的数字,并产生新的数字
int er=0;
int flag=0;
while(er<7){
flag=0;
temp=LotteryNumber[er];

for(int i=0;i<LotteryNumber.length-1;i++){
if(temp==LotteryNumber[i]&&er!=i){
temp=CreateLotterNumber();
flag=1;
break;
}
}
if(flag==0) {
er+=1;
}else{
LotteryNumber[er]=temp;
}
}
// end

for(int j=0;j<LotteryNumber.length-1;j++) //重新排序
for(int i=j+1;i<LotteryNumber.length;i++)
if(LotteryNumber[j]>LotteryNumber[i]){
temp=LotteryNumber[j];
LotteryNumber[j]=LotteryNumber[i];
LotteryNumber[i]=temp;
}

for(int j=0;j<LotteryNumber.length;j++)
System.out.print(LotteryNumber[j]+"\t");
}

public static int CreateLotterNumber(){
int temp;
Random random1;
random1=new Random(java.lang.System.currentTimeMillis());
temp=(random1.nextInt())%37;

while(temp==0){
random1=new Random(java.lang.System.currentTimeMillis());
temp=(random1.nextInt())%37;
}

temp=temp>0?temp:-1*temp;
return temp;
}

}


class LotteryNumberTheard extends Thread{
GetLotteryNumber GetLottery=new GetLotteryNumber();
Random random;
LotteryNumberTheard(int ArraryNumber){
setLotterNumber(ArraryNumber);
}

public void setLotterNumber(int ArrayNumber){
int ArNumber=ArrayNumber;
int temp;
random=new Random(java.lang.System.currentTimeMillis());
temp=(random.nextInt())%37;

while(temp==0){
random=new Random(java.lang.System.currentTimeMillis());
temp=(random.nextInt())%37;
}

temp=temp>0?temp:-1*temp;
GetLottery.LotteryNumber[ArNumber]=temp;
}
}

使用:
D:\JavaWork>javac GetLotteryNumber.java

D:\JavaWork>java -classpath . GetLotteryNumber
彩票号码为:3 7 9 14 26 32 35
D:\JavaWork>java -classpath . GetLotteryNumber 3
第1组彩票号码为: 5 9 10 13 19 24 36
第2组彩票号码为: 2 4 6 15 25 34 36
第3组彩票号码为: 2 14 21 24 27 30 31



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: