JAVA面向对象「双色球案例」

JAVA面向对象编程[双色球案例]…

面向对象双色球案例说明:

根据福利彩票标准中奖规则:

在这里插入图片描述

代码如下:

//工具类
package TwoColorBall;
import java.util.*;
/**
 * 双色球
 * 1,用户选择机选还是首选
 * 2,接收用户选号(6红1蓝)
 * 3,生成机选(6红1蓝)
 * 4,比较系统代码和用户代码
 * 5,验证是否中奖
 * 7,公布结果
 * @author Fang
 */
public class toolMethod {
    //变量
    int[] userRedBall = new int[6];//用户选择的红球
    int[] sysRedBall = new int[6];//机选红球
    int userBlueBall = 0;//用户选择的篮球
    int sysBlueBall = 0;//机选篮球
    int redCount = 0;//记录正确
    int blueCount = 0;//
    int[] redBall = new int[33];//存储红球1-33
    //随机生成6个1-33不同的数字
    public void Redballrandom() {
        for (int i = 0; i < redBall.length; i++) {
            redBall[i] = i + 1;
        }
    }
    public static void computerSelection(int[] redBall, int[] useerredBall) {
        Random r = new Random();
        int index = -1;
        for (int i = 0; i < useerredBall.length; i++) {
            index = r.nextInt(redBall.length - i);
            useerredBall[i] = redBall[index];
            int temp = redBall[index];
            redBall[index] = redBall[redBall.length - 1 - i];
            redBall[redBall.length - 1 - i] = temp;
        }
    }
    public static void sort(int[] ball){//冒泡排序
        for (int i = 0; i < ball.length; i++) {
            for (int j = 0; j < ball.length-1-i; j++) {
                if (ball[j] > ball[j+1]) {
                    ball[j]=ball[j]+ball[j+1];
                    ball[j+1]=ball[j]-ball[j+1];
                    ball[j]=ball[j]-ball[j+1];
                }
            }
        }
    }
}


## 测试类:
package TwoColorBall;
import java.util.*;
public class TwoColorBallTese {
    public static void main(String[] args) {
        Random r = new Random();
        toolMethod io = new toolMethod();
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < io.redBall.length; i++) {
            io.redBall[i] = i + 1;
        }
        System.out.println("欢迎购买双色球");
        System.out.println("1.机选");
        System.out.println("2.手动选择");
        int numshuru = input.nextInt();
        boolean pp = true;
        while (pp) {
            switch (numshuru) {
                case 1:
                    io.computerSelection(io.redBall, io.userRedBall);
                    io.userBlueBall = r.nextInt(16) + 1;
                    pp=false;
                    break;
                case 2:
                    System.out.println("请选择6个红球号码(1-33):");
                    for (int i = 0; i < io.userRedBall.length; i++) {
                        io.userRedBall[i] = input.nextInt();
                    }
                    System.out.println("请选择一个篮球号码(1—16):");
                    io.userBlueBall = input.nextInt();
                    pp = false;
                    break;
                default:
                    System.out.println("欢迎购买双色球");
                    System.out.println("1.机选");
                    System.out.println("2.手动选择");
                    break;
            }
        }
        io.computerSelection(io.redBall, io.sysRedBall);
        io.sysBlueBall = r.nextInt(16) + 1;
        //结果
        //统计红球
        for (int i = 0; i < io.userRedBall.length; i++) {
            for (int j = 0; j < io.sysRedBall.length; j++) {
                if (io.userRedBall[i] == io.sysRedBall[j]) {
                    int temp = io.sysRedBall[j];
                    io.sysRedBall[j] = io.sysRedBall[io.sysRedBall.length - 1 - io.redCount];
                    io.sysRedBall[io.sysRedBall.length - 1 - io.redCount] = temp;
                    io.redCount++;
                    break;
                }
            }
        }
        if (io.userBlueBall == io.sysBlueBall) {
            io.blueCount = 1;
        }
        //验证是否中奖
        if (io.blueCount == 0 && io.redCount <= 3) {
            System.out.println("未中奖");
        } else if (io.blueCount == 1 && io.redCount < 3) {
            System.out.println("六等奖:五元钱");
        } else if (io.blueCount == 1 && io.redCount == 3 || io.blueCount == 0 && io.redCount == 4) {
            System.out.println("五等奖:十元钱");
        } else if (io.blueCount == 1 && io.redCount == 4 || io.blueCount == 0 && io.redCount == 5) {
            System.out.println("四等奖:二百元");
        } else if (io.blueCount == 1 && io.redCount == 5) {
            System.out.println("三等奖:3000元");
        } else if (io.blueCount == 0 && io.redCount == 6) {
            System.out.println("二等奖:150w");
        }else if (io.blueCount == 1 && io.redCount == 6) {
            System.out.println("一等奖:500w");
        }else {
            System.out.println("系统有误,白玩");
        }
        System.out.println("本期中奖红球号码为:");
        io.sort(io.sysRedBall);
        System.out.println(Arrays.toString(io.sysRedBall));
        System.out.println("本期中奖的篮球号码为:"+io.sysBlueBall);
        //用户号码
        System.out.println("您选择的红球号码为:");
        io.sort(io.userRedBall);
        System.out.println(Arrays.toString(io.userRedBall));
        System.out.println("您选择的篮球号码为:"+io.userBlueBall);`在这里插入代码片`
        System.out.println("感谢购买");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值