爱心技术专栏专题

成程序代码

摘录:JspServlet 来源:JspServlet 加入时间:2006年09月03日
摘要:
成程序代码

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

public class GetLotteryNumber{
public static int[] LotteryNumber=new int[7];
public static int temp=0;
stat…

成程序代码

站点:爱心种子小博士 关键字:成程序代码
成程序代码
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

转载:转载请保留本信息,本文来自http://www.51dibs.com/lp07/la/a20/l_a_efd59bd0f360136e.html

客户服务中心信箱:[email protected] [email protected] 网站地图

声明

爱心赞助: