java练习,模拟双色球--学习笔记

题目:模拟双色球标准下注

思路:创建俩个数组,一个负责选号,一个摇号,最后对比看中将结果

代码:

package com.zeyu.lianxi;
import java.util.Scanner;
import java.util.Random;

public class union_lotto {
    public static void main(String[] args) {
        int[] arr1 = new int[7];
        int[] arr2 = new int[7];

        numerical_selection(arr1);  //选号
        lottery(arr2);  //摇号
        String bonus = run_a_lottery(arr1,arr2);    //开奖
        result(arr1,arr2,bonus);    //结果
    }

    public static void numerical_selection(int[] arr){  //选号
        Scanner sc = new Scanner(System.in);

        for (int i = 0; i < arr.length; ) {
            boolean blue = false;
            if(i+1 == arr.length) blue = true;  //当前选号是否为蓝球号码
            System.out.print(blue?"请选择篮球号码(1-16):":"请选择第"+(i+1)+"个红球号码(1-33):");
            int num = sc.nextInt();

            if(blue?(num > 16 || num < 1):(num > 33 || num < 1)){  //号码是否合法
                System.out.println(blue?"请选择1-16之内的号码!":"请选择1-33之内的号码!");
                continue;   //不合法重新选
            }

            if(!blue)   //非蓝球查重
                if(dp_check(arr, num)){
                    System.out.println("号码重复!");
                    continue;   //重复了重新选
                }

                arr[i] = num;  //不重复且合法就确定号码
                i++;   //号码不重复并且合法才进行下一个选号

        }
    }

    public static void lottery(int[] arr){  //摇号
        Random r = new Random();

        for (int i = 0; i < arr.length; ) {
            if(i+1 != arr.length) { //是否为蓝球
                int num = r.nextInt(1, 34); //随机出中将号码
                if(dp_check(arr, num)){ //查重
                    continue;   //重复了重新摇
                }
                arr[i] = num;   //不重复确定号码
                i++;
            }

            else
                arr[i++] = r.nextInt(1,17); //蓝球情况,直接摇号无需查重
        }
    }

    public static String run_a_lottery(int[] arr1, int[] arr2){ //开奖
        int red = 0;    //红球中了多少个
        boolean blue = false;   //篮球中了没有

        for (int i = 0; i < arr1.length - 1; i++) { //统计红球中将数
            for (int j = 0; j < arr1.length - 1; j++) {
                if(arr1[i] == arr2[j])
                {red++;break;}
            }
        }

        if(arr1[arr1.length-1] == arr2[arr2.length-1])  //判断篮球
            blue = true;

        System.out.println("red:"+red+" blue:"+blue);   //输出结果

        switch (red){   //选择中将金额
            case 0: case 1:
                if(blue) return "5";
                break;
            case 2: case 3:
                if(blue) return "10";
                break;
            case 4:
                if(blue) return "200";
                else return "10";
            case 5:
                if(blue) return "3000";
                else return "200";
            case 6:
                if(blue) return "1000万";
                else return "500万";
        }

        return "0"; //没有中将
    }

    public static boolean dp_check(int[] arr, int num){ //查重
        int j = 0;
        while(j < arr.length){
            if(num == arr[j])
                break;
            j++;
        }

        return j != arr.length;
    }

    public static void result(int[] arr1, int[] arr2, String bonus){    //公布结果
        System.out.println("您的号码:");
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(i +1 != arr1.length?arr1[i] + "\t":"|"+arr1[i]+"|\n");
        }

        System.out.println("中奖号码:");
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(i +1 != arr2.length?arr2[i] + "\t":"|"+arr2[i]+"|\n");
        }

        if(bonus.equals("0"))
            System.out.println("很可惜,您没有中将");
        else
            System.out.println("恭喜您,获奖"+bonus+"元!");
    }

}

运行结果

java练习,模拟双色球--学习笔记_第1张图片

你可能感兴趣的:(学习笔记,java,学习,笔记)