Java实现一个简单的双色球系统


import java.util.Arrays;
import java.util.Scanner;
public class twoColorBallTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 用来记录本次客官购买的号码数
        // 如果客官从新购买则记录最新的
        int[] purchased = null;
        // 用来记录最新的本期的开奖
        int[] lottery = null;
        o:
        while (true) {
            System.out.println("欢迎使用双色球购买系统, 官方网站www.shuangseqiu.com");
            System.out.println("******请输入菜单选项******");
            System.out.println("       1:购买双色球      ");
            System.out.println("       2:查看开奖      ");
            System.out.println("       3:查看中奖      ");
            System.out.println("       4:退出系统      ");
            int option = scanner.nextInt();
            switch (option) {
                case 1:
                    int[] purchasedNumber = purchase();
                    purchased = Arrays.copyOf(purchasedNumber, 7);
                    System.out.println("您购买的号码如下:");
                    System.out.println(Arrays.toString(purchasedNumber));
                    break;
                case 2:
                    System.out.println("本期开奖号码是:");
                    int[] lotteryNumber = getLottery();
                    System.out.println(Arrays.toString(lotteryNumber));
                    lottery = Arrays.copyOf(lotteryNumber,7);
                    break;
                case 3:
                    System.out.println("客官您购买的号码是:");
                    System.out.println(Arrays.toString(purchased));
                    System.out.println("本期开奖奖号码是:");
                    System.out.println(Arrays.toString(lottery));
                    System.out.println("您的中奖号码是:");
                    winningList(purchased, lottery);
                    break;
                case 4:
                    System.out.println("客人常来呀~~~");
                    // 退出系统直接结束 o 的循环
                    break o;
                default:
                    System.out.println("输入有误");
            }

        }

    }

    /**
     * 判断客官是否中奖,中奖的话是几等奖
     * @param purchased 客官购买的号码
     * @param lottery 开奖号码
     */
    public static void winningList(int[] purchased, int[] lottery) {
        if (purchased == null ) {
            System.out.println("客官先购买号码哦~~~");
            return;
        }
        if (lottery == null) {
            System.out.println("客官请先查看开奖号码哦~~~");
            return;
        }
        // 红球中奖个数
        int red = 0;
        // 蓝球是否中
        boolean isBlue = false;
        int blue = 0;
        // 返回中奖的号码
        int[] win = new int[7];
        int index = 0;
        if (purchased[6] == lottery[6]) {
            isBlue = true;
            blue = 1;
        }
        for (int i = 0; i < purchased.length - 1; i++) {
            for (int j = 0; j < lottery.length - 1; j++) {
                if (purchased[i] == lottery[j]) {
                    win[index] = purchased[i];
                    red++;
                    index++;
                }
            }
        }
        if (index == 0 && !isBlue) {
            System.out.println("很遗憾您没有中奖~~~");
            return;
        }
        if (isBlue) {
            win[index] = lottery[6];
        }

        for (int i = 0; i < red + blue; i++) {
            System.out.print(win[i] + "\t");
        }
        System.out.println();
        if (red == 6) {
            if (isBlue) {
                System.out.println("恭喜客官中了一等奖!!! 获得奖金50000元");
            } else {
                System.out.println("恭喜客官中了二等奖!!! 获得奖金40000元");
            }
        } else if (red == 5) {
            if (isBlue) {
                System.out.println("恭喜客官中了三等奖!!! 获得奖金30000元");
            } else {
                System.out.println("恭喜客官中了四等奖!!! 获得奖金20000元");
            }
        } else if (red == 4) {
            if (isBlue) {
                System.out.println("恭喜客官中了四等奖!!! 获得奖金20000元");
            } else {
                System.out.println("恭喜客官中了五等奖!!! 获得奖金10000元");
            }
        } else if (red == 3) {
            if (isBlue) {
                System.out.println("恭喜客官中了五等奖!!! 获得奖金10000元");
            } else {
                System.out.println("很遗憾客官您没中奖");
            }
        } else if (red == 2 || red == 1 || red == 0) {
            if (isBlue) {
                System.out.println("恭喜客官中了六等奖!!! 获得奖金5000元");
            } else {
                System.out.println("很遗憾客官您没中奖");
            }
        }

    }

    /**
     * 用户购买双色球的方法
     * @return 返回用户购买的双色球数组
     */
    public static int[] purchase() {
        Scanner scanner = new Scanner(System.in);
        int[] op = new int[7];
        int i = 1;
        while (i < 7) {
            System.out.println("请输入你要购买的第" + i + "个红色球号码");
            int num = scanner.nextInt();
            if (isTheRightNumber(num, i)) {
                if (isRepeat(op, num)) {
                    System.out.println("请不要重复购买号码~");
                } else {
                    op[i - 1] = num;
                    i++;
                }
            } else {
                System.out.println("红球输入的号码有误哦~");

            }

        }
        System.out.println("请输入要购买的蓝球号码:");
        int num = scanner.nextInt();
        while (!isTheRightNumber(num,7)) {
            System.out.println("蓝球输入的号码有误,请重新输入:");
            num = scanner.nextInt();
        }
        op[op.length - 1] = num;
        //System.out.println("您购买的号码如下:");
        //System.out.println(Arrays.toString(op));
        return op;
    }

    /**
     * 判断红球号码输入是否正确
     * @param num 购买的号码
     * @param i i<7 代表判断购买的红球号码是否正确,i=7代表判断购买的蓝球号码是否正确
     * @return 正确为true,错误为false
     */
    public static boolean isTheRightNumber(int num, int i) {
        boolean flag = true;
        if (i < 7) {
            if (num < 1 || num > 33) {
                flag = false;
            }
        } else {
            if (num < 1 || num > 16) {
                flag = false;
            }
        }
        return flag;
    }

    /**
     * 双色球开奖的方法
     * 双色球的规则
     * 前六位红球1-33不能重复升序排列
     * 最后一位蓝球1-16 可以和前六位重复
     * @return 返回本期开奖的双色球数组
     */
    public static int[] getLottery() {
        int[] lottery = new int[6];
        int i = 0;
        while (i < 6) {
            int num = (int) (Math.random() * 33 + 1);
            int temp = i;
            if (i == 0) {
                lottery[i] = num;
                i++;
            } else {
                // 判断前面的数组是否包含了该随机数
                // 用来标记是否有相同的数
                //boolean flag = false;
                /*for (int j = 0; j < i; j++) {
                    if (lottery[j] == num) {
                        // 有则标记为true,退出for循环
                        flag = true;
                        break;
                    }
                }*/
                if (isRepeat(lottery,num)) {
                    // 保持i值不变 继续执行while
                    i = temp;
                } else {
                    lottery[i] = num;
                    i++;
                }
            }
        }
        Arrays.sort(lottery);
        int[] newLottery = Arrays.copyOf(lottery,7);
        int num = (int) (Math.random() * 33 + 1);
        newLottery[newLottery.length-1] = num;

        return  newLottery;
    }

    /**
     * 判断号码是否有重复的
     * @param arr 已经输入的号码
     * @param num 即将输入的号码
     * @return 有重复则返回false
     */
    public static boolean isRepeat(int[] arr, int num) {
        boolean flag = false;
        for (int i : arr) {
            if (i == num) {
                flag = true;
            }
        }
        return flag;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值