java程序模拟双色球开奖

package com.method1;

import java.text.DecimalFormat;
import java.util.Random;

/**
 * @author: Mr.MKB
 * @Version: V1.0
 * @Description: 测试双色球机选多少次能中大奖
 * @DATE: 2018-09-04 11:44
 * @throws
 */
public class test6 {

    public static void main(String[] args) {
        boolean fs = true;
        int noprice = 0; //未中奖
        int accout = 0;  //购彩次数
        int sixprice = 0; //六等奖中奖次数
        int fiveprice = 0; //五等奖中奖次数
        int fourprice = 0; //四等奖中奖次数
        int threeprice = 0; //三等奖中奖次数
        int twoprice = 0; //二等奖中奖次数
        int numone = 0;   //一等奖中奖次数
        while (fs) {
            int[] userRedBall = new int[6];//用户选择的红球号码
            int[] sysRedBall = new int[6]; //系统开奖的红球号码
            int userBlueBall = 0; //用户选择的蓝球号码
            int sysBlueBall;  //系统开奖的蓝球号码
            int redCount = 0;  //记录用户选择正确的红球数:
            int blueCount = 0; //记录用户选择正确的蓝球数;

            int[] redBall = new int[33]; //用于存储1-33的红球号码
            Random r = new Random();
            //
            for (int i = 0; i < redBall.length; i++) {
                redBall[i] = i + 1;
            }

            //用户机选
            //机选蓝球
            computerSelection(redBall, userRedBall);
            //机选蓝球
            userBlueBall = r.nextInt(16) + 1;

            //系统开奖号码:
            //生成系统开奖红球
            computerSelection(redBall, sysRedBall);
            sysBlueBall = r.nextInt(16) + 1;

            //统计结果 1.统计红球
            for (int i = 0; i < userRedBall.length; i++) {
                for (int j = 0; j < sysRedBall.length; j++) {
                    if (userRedBall[i] == sysRedBall[j]) {
                        int temp = sysRedBall[j];
                        sysRedBall[j] = sysRedBall[sysRedBall.length - 1 - redCount];
                        sysRedBall[sysRedBall.length - 1 - redCount] = temp;
                        redCount++;
                        break;
                    }
                }
            }

            if (sysBlueBall == userBlueBall) {
                blueCount = 1;
            }
            if (blueCount == 0 && redCount <= 3) {
                noprice++;
            } else if (blueCount == 1 && redCount < 3) {
                sixprice++;
            } else if ((blueCount == 1 && redCount == 3) || (blueCount == 0 && redCount == 4)) {
                fiveprice++;
            } else if ((blueCount == 1 && redCount == 4) || (blueCount == 0 && redCount == 5)) {
                fourprice++;
            } else if (blueCount == 1 && redCount == 5) {
                threeprice++;
            } else if (blueCount == 0 && redCount == 6) {
                twoprice++;
            } else if (blueCount == 1 && redCount == 6) {
                numone++;
                System.out.print("系统开奖号码为");
                for (int x : sysRedBall) {
                    System.out.print(x + " ");
                }
                System.out.print(sysBlueBall);
                System.out.println();
                System.out.print("机选结果为");
                for (int y : userRedBall) {
                    System.out.print(y + " ");
                }
                System.out.println(userBlueBall);
                if (numone < 10) {
                    continue;
                } else {
                    fs = false;
                }
            } else {
                System.out.println("系统有误,中奖无效");
            }
            accout++;
        }
        System.out.println("统计结果为:");
        System.out.println("总机选次数:" + accout + "   购彩金额:" + (accout * 2));
        System.out.println("未中奖次数:" + noprice);
        System.out.print("六等奖次数:" + sixprice + "  六等奖金额: 5" + "   奖金总金额:" + (sixprice * 5));
        toDoubless(sixprice, accout);
        System.out.print("五等奖次数:" + fiveprice + "  五等奖金额: 10" + "   奖金总金额:" + (fiveprice * 10));
        toDoubless(fiveprice, accout);
        System.out.print("四等奖次数:" + fourprice + "  四等奖金额: 200" + "   奖金总金额:" + (fourprice * 200));
        toDoubless(fourprice, accout);
        System.out.print("三等奖次数:" + threeprice + "  三等奖金额: 3000" + "   奖金总金额" + (threeprice * 3000));
        toDoubless(threeprice, accout);
        System.out.print("二等奖次数:" + twoprice + "  二等奖金额:  "+(int)(accout*0.4/twoprice) + "  奖金总金额" + (int) (accout * 0.4));
        toDoubless(twoprice, accout);
        System.out.print("一等奖:" + numone + "  一等奖金额: 5000000" + "      奖金总金额:" + (numone * 5000000));
        toDoubless(numone, accout);
        System.out.print("总中奖金额:" + (numone * 5000000 + (int) (accout * 0.4) + threeprice * 3000 +
                fourprice * 200 + fiveprice * 10 + sixprice * 5));
        System.out.println("彩票店盈利金额:" + (accout * 2 - (numone * 5000000 + (int) (accout * 0.4) + threeprice * 3000 +
                fourprice * 200 + fiveprice * 10 + sixprice * 5)));
    }

    //用于在指定数列中,随机生成多个不重复的数算法
    public static void computerSelection(int[] balls, int[] redBall) {
        Random r = new Random();
        int index = -1;
        for (int i = 0; i < redBall.length; i++) {
            index = r.nextInt(balls.length - i);
            redBall[i] = balls[index];
            int temp = balls[index];
            balls[index] = balls[balls.length - 1 - i];
            balls[balls.length - 1 - i] = temp;
        }
    }

    public static void toDoubless(int a, int b) {
        double ss = (float) a / b * 100;
        DecimalFormat ds = new DecimalFormat("0.000000");//格式化小数
        String tosys = ds.format(ss);//返回String类型
        System.out.println("   中奖概率" + tosys + "%");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值