Java编程题 —— 双色球


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

public class DoubleColorBalls_v1 {
    /*
    双色球随机选号(6+1)
    1.红球:01~33里随机取出六个,不能重复,按照升序排序
     */
    public static void main(String[] args) {
        String[] red = new String[33];
        for (int i = 1; i <= 33; i++) {
            if (i<10){
                red[i-1]="0"+i;
            }else {
                red[i-1] =i+"";
            }
        }
        System.out.println(Arrays.toString(red));
        String[] bluePool = new String[16];
        bluePool = Arrays.copyOf(red,bluePool.length);
        System.out.println(Arrays.toString(bluePool));
        String[]  awardnum = new String[6];

//        Random rd = new Random();
//        for (int i = 0; i < 6; i++) {
//            int num = rd.nextInt(33)+1;
//            if (num<10){
//                awardnum[i]="0"+num;
//            }else {
//                awardnum[i]=num+"";
//            }
//        }
//        Arrays.sort(awardnum);
//        boolean b = false;
//        while(true){
//            String[] arr2 = Arrays.copyOf(awardnum,awardnum.length); //设置对比
//            for (int i = 0; i < 5; i++) {//判断是否有重复,如果重复则重新抽取
//                if (awardnum[i].equals(awardnum[i + 1])) {
//                    String s= awardnum[i];
//                    int num = rd.nextInt(33)+1;
//                    if (num<10){
//                        awardnum[i]="0"+num;
//                    }else {
//                        awardnum[i]=num+"";
//                    }
//                    while (true) {//防止修改后与修改前是同一值
//                        if (awardnum[i].equals(s)) {
//                            if (num < 10) {
//                                awardnum[i] = "0" + num;
//                            } else {
//                                awardnum[i] = num + "";
//                            }
//                        }else {
//                            break;
//                        }
//                    }
//                }
//            }
//            if (Arrays.equals(arr2, awardnum)){//判断是否有重复值
//                break;
//            }
//        }
//        Arrays.sort(awardnum);

        Random rd = new Random();
        for (int i = 0; i < 6; i++) {
//            Arrays.sort(red);
//            int num = rd.nextInt(red.length)+1;
//            if (num<10){
//                awardnum[i]="0"+num;
//            }else {
//                awardnum[i]=num+"";
//            }
            //随机下标
            int index = rd.nextInt(red.length);
            //通过下标取出数字
            awardnum[i]=red[index];
            //取出覆盖
            red[index] = red[red.length-1];
            //减容
            red = Arrays.copyOf(red,red.length-1);
        }
        System.out.println(Arrays.toString(awardnum));
        //随机取一个蓝球
        String blueBall = bluePool[rd.nextInt(bluePool.length)];
        //扩容
        awardnum = Arrays.copyOf(awardnum,awardnum.length+1);
        //加上取出蓝球值
        awardnum[awardnum.length-1]=blueBall;
        System.out.println(Arrays.toString(awardnum));
    }
}
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class DoubleColorBalls_v2 {
    public static void main(String[] args) {
        /*
        需求:
        控制台输入购买双色球号码的注数
        打印对应注数的双色球号码
         */
//        System.out.println(Arrays.toString(gen()));
        String[] awn = gen();
        boolean b=false;
        Scanner sc =new Scanner(System.in);
        System.out.println("请输入购买注数");
        int num  = sc.nextInt();
        int n=0;
        System.out.println("本次双色球的号码为:"+Arrays.toString(awn));
        System.out.println("————————————————————");
        while (n<num){
            String[] Buynum = gen();
            System.out.println("你所购买的双色球号码为:"+Arrays.toString(Buynum));

            if (Arrays.equals(awn,Buynum)){
                b=true;
//                System.out.println("恭喜你中奖了,中奖号码为:"+Arrays.toString(awn));
            }
            n++;
        }
        System.out.println("————————————————————");
        if (b){
            System.out.println("恭喜你中奖了,中奖号码为:"+Arrays.toString(awn));
        }else{
            System.out.println("很遗憾你没有中奖,请等待下一期");
        }
    }

    //贴标签
    public static String[] gen(){
        String[] red = new String[33];
        String[] blue = new String[16];
        String[] awardnum = new String[6];
        int count=0;
        //红球
        for (int i = 1; i <= 33; i++) {
            if (i<10){
                red[i-1]="0"+i;
            }else {
                red[i-1] =i+"";
            }
        }
        //蓝球
        blue = Arrays.copyOf(red,16);
        boolean[] bl = new boolean[33];

        Random rd = new Random();
        while (true){
            int num;
            if (count<5) {
                num = rd.nextInt(red.length)+1;
                if (bl[num - 1]&&count<5){
                    continue;
                }
                if (num < 10) {
                    awardnum[count] = "0" + num;
                } else {
                    awardnum[count] = num + "";
                }
                bl[num - 1] = true;
                count++;
            }else{
                num = rd.nextInt(blue.length)+1;
                if (num < 10) {
                    awardnum[count] = "0" + num;
                } else {
                    awardnum[count] = num + "";
                }
                count++;
                break;
            }
        }
        return awardnum;
    }
}

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

public class DoubleColorBalls_v2_2 {
    public static void main(String[] args) {
        /*
        需求:
        控制台输入购买双色球号码的注数
        打印对应注数的双色球号码
         */
//        System.out.println(Arrays.toString(gen()));
        String[] awn = gen();
        boolean b=false;
        Scanner sc =new Scanner(System.in);
        System.out.println("请输入购买注数");
        int num  = sc.nextInt();
        int n=0;
        System.out.println("本次双色球的号码为:"+Arrays.toString(awn));
        System.out.println("————————————————————");
        while (n<num){
            String[] Buynum = gen();
            System.out.println("你所购买的双色球号码为:"+Arrays.toString(Buynum));

            if (Arrays.equals(awn,Buynum)){
                b=true;
//                System.out.println("恭喜你中奖了,中奖号码为:"+Arrays.toString(awn));
            }
            n++;
        }
        System.out.println("————————————————————");
        if (b){
            System.out.println("恭喜你中奖了,中奖号码为:"+Arrays.toString(awn));
        }else{
            System.out.println("很遗憾你没有中奖,请等待下一期");
        }
    }

    //贴标签
    public static String[] gen(){
        String[] red = new String[33];
        String[] blue = new String[16];
        String[] balls = new String[6];
        Arrays.fill(balls,"34");//填补避免空指针,在范围外,避免影响排序
        int count=0;
        //红球
        for (int i = 1; i <= 33; i++) {
            if (i<10){
                red[i-1]="0"+i;
            }else {
                red[i-1] =i+"";
            }
        }
        //蓝球
        blue = Arrays.copyOf(red,16);
        boolean[] bl = new boolean[33];

        Random rd = new Random();
        while (true){
            int num;
            if (count<6) {
                //随即下标
                num = rd.nextInt(red.length);
                if (bl[num]){ continue; }
                //随机下标取红球到球管
                balls[count++]=red[num];
                //标记
                bl[num] = true;Arrays.sort(balls);
            }else{
                balls = Arrays.copyOf(balls,balls.length+1);
                num = rd.nextInt(blue.length);
                balls[count]=blue[num];
                break;
            }
        }
        return balls;
    }
}

你可能感兴趣的:(记录,java)