首页 > 代码库 > java基础知识应用--双色球开奖号码

java基础知识应用--双色球开奖号码

  双色球是中国福利彩票的玩法,双色球分为红色球号码区和蓝色球号码区,红色球号码区由1-33共33个号码组成,蓝色球号码区由1-16共16个号码组成,开奖号码由6个不重复的红色球号码和1个蓝色球号码共7个数组成。

  首先要设置三个数组来分别保存所有球的号码、抽取红球的号码和标记抽取球的号码,然后抽取不重复的随机的6个红色球号码和1个蓝色球号码,最后给红色球排序与蓝色球一起输出。需要注意的是号码是01-33,因此1-9的值要补“0”

import java.util.Arrays;

import java.util.Random;

public class Test01 {

       /**

        *双色球:共有7个数字

        *红球:6  每个都不重复,数的范围在1~33

        *蓝球:1范围1~16

        */

       publicstatic void main(String[] args) {

              Randomr=new Random();

              //1、定义球池:

              Stringballs[]=new String[33];

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

                     if(i+1<10){

                            balls[i]="0"+(i+1);//1~9前面补零

                            }else{

                            balls[i]=""+(i+1);

                     }

              }

              System.out.println(Arrays.toString(balls));

             

              //定义存放红球的数组,还有存放蓝球的变量

              Stringred[] =new String [6];

              Stringblue;

             

              //定义一个布尔值数组,用来标记红球中的球是否已经被抽取过,false表示未被抽中

              boolean[] flags=new boolean[33];

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

                     flags[i]=false;

              }

             

             

              //抽取红球

              intindex;//红球下标变量

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

                     do{

                            index=r.nextInt(33);//index=3 抽取了04号球

                     }while(flags[index]);

                    

                     red[i]=balls[index];

                     flags[index]=true;//改标记:如果对应的数被抽中就做标记

              }

              Arrays.sort(red);

             

             

              //抽取蓝球

              blue=balls[r.nextInt(16)];

             

 

              //输出抽奖号码

              System.out.print("本期开奖号码:");

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

                     System.out.print(red[i]+"");

              }

              //数组的复制-Arrays.copyof(原数组,新数组长度)

              StringdoubleBalls[]=Arrays.copyOf(red,red.length+1);

              doubleBalls[doubleBalls.length-1]=blue;

              System.out.println("本期开奖号码:"+Arrays.toString(doubleBalls));

       }

}

 

 


java基础知识应用--双色球开奖号码