Java版的生成双色球程序

package chap1;

import java.util.Arrays;
import java.util.Random;

public class DoubleBall {

public static void getBall(int[] reds,int[] blues){

//创建生成随机数的对象
Random random  = new Random();
//定义红色球
int[] redBall = new int[6];
//定义蓝色球
int blueBal = 0;
//定义游标
int index = 0;

for(int i=0;i<redBall.length;i++){

index = random.nextInt(33);
redBall[i] = reds[index];

for(int j=0;j<i;j++){

//判断是否有相同的红球
if(redBall[i] == redBall[j]){

  i--;
  break;
}
}
}

//给蓝色求赋值
blueBal = random.nextInt(17);
//红球排序,按从小到大的顺序
Arrays.sort(redBall);
System.out.print("红色球为:"+Arrays.toString(redBall));
System.out.println("蓝色球为:"+blueBal);
}

public static void main(String[] args) {

int[] reds = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33};
int[] blues = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

for(int i=0;i<5;i++){

DoubleBall.getBall(reds, blues);
}

}
}

posted on 2012-03-12 18:09  Msea  阅读(542)  评论(0编辑  收藏  举报

导航