java基础练习之模拟类似双色球小系统

java基础练习之模拟类似双色球小系统



前言

使用java基础做一个模拟类似双色球的系统,该系统仅供参考,不是真实的双色球,娱乐而已,实现方法也不只这一种.


一、代码演示


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

public class TwoColorBall {
    public static void main(String[] args) {
        //用户选的的红球与篮球
        int[] userRedBall=new int[6];
        int userBlueBall=0;
        //电脑随机生成的红球与篮球
        int[] compRedBall=new int[6];
        int compBlueBall=0;

        Scanner input=new Scanner(System.in);
        Random r=new Random();
        System.out.println("请选择机选还是自选:(1:机选---2:自选)");
        int temp=input.nextInt();
        while (temp!=0){
            switch (temp){
                case 1:
                    //机选
                    ByContBall(userRedBall);
                    userBlueBall=r.nextInt(16)+1;
                    ByContBall(compRedBall);
                    compBlueBall=r.nextInt(16)+1;
                    ByIf(userRedBall,userBlueBall,compRedBall,compBlueBall);
                    temp=0;
                    break;
                case 2:
                    //自选
                    System.out.println("请输入6个红球:");
                    for (int i=0;i<6;i++){
                        userRedBall[i]=input.nextInt();
                    }
                    System.out.println("请输入一个蓝球:");
                    userBlueBall=input.nextInt();

                    ByContBall(compRedBall);
                    compBlueBall=r.nextInt(16)+1;
                    ByIf(userRedBall,userBlueBall,compRedBall,compBlueBall);
                    temp=0;
                    break;
                default:
                    System.out.println("请重新选择");
                    System.out.println("请选择机选还是自选:(1:机选---2:自选)");
                    temp=input.nextInt();
            }
        }
    }
    public static void ByContBall(int[] redBall){
        int[] comp=new int[33];
        for (int i=0;i<33;i++){
            comp[i]=i+1;
        }
        //随机数工具类
        Random r=new Random();
        for (int i=0;i<redBall.length;i++){
            //随机选择comp数组中的数据
            int index=r.nextInt(comp.length-1-i);
            redBall[i]= comp[index];
            //将选择过的数据放到数组最后面,将最后面的数据向前方插入
            comp[index]=comp[index]+comp[comp.length-1-i];
            comp[comp.length-1-i]=comp[index]-comp[comp.length-1-i];
            comp[index]=comp[index]-comp[comp.length-1-i];
        }
    }
    //判断中奖情况
    public static void ByIf(int[] userRed,int userBlue,int[] compRed,int compBlue){
        Arrays.sort(userRed);
        Arrays.sort(compRed);
        int red=0,blue=0;
        for (int i=0;i<userRed.length;i++){
            for (int j=0;j<compRed.length;j++){
                if (compRed[j]==userRed[i]){
                   red++;
                   break;
                }
            }
        }
        if (userBlue==compBlue){
            blue+=1;
        }
        if (red<=4){
            System.out.println("本期中奖号码:红球:"+Arrays.toString(compRed)+"蓝球:"+compBlue);
            System.out.println("您的选号为:红球:"+Arrays.toString(userRed)+"蓝球"+userBlue);
            System.out.println("您的选中为:红球"+red+"蓝球"+blue);
            System.out.println("再接再厉!");
        }else if (red==5){
            System.out.println("本期中奖号码:红球:"+Arrays.toString(compRed)+"蓝球:"+compBlue);
            System.out.println("您的选号为:红球:"+Arrays.toString(userRed)+"蓝球"+userBlue);
            System.out.println("**********恭喜*************三等奖,奖金50万");
        }else if (red==6 && blue==0){
            System.out.println("本期中奖号码:红球:"+Arrays.toString(compRed)+"蓝球:"+compBlue);
            System.out.println("您的选号为:红球:"+Arrays.toString(userRed)+"蓝球"+userBlue);
            System.out.println("**********恭喜*************二等奖,奖金100万");
        }else if (red == 6){
            System.out.println("本期中奖号码:红球:"+Arrays.toString(compRed)+"蓝球:"+compBlue);
            System.out.println("您的选号为:红球:"+Arrays.toString(userRed)+"蓝球"+userBlue);
            System.out.println("**********恭喜*************一等奖,奖金1000万");
        }
    }

}


二.小结

本章概述了一个小系统,还是蛮有意思的,做好了不仅能提升java基础,还能闲着无聊来一注,碰一下运气,娱乐娱乐,仅供参考.
有哪里不足或者有更好的建议,欢迎留言吐槽,有哪里不懂的小伙伴,可以私信我,我会一一答复,感谢认可,感谢支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

道而起

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值