双色球计算命中概率代码(Java)

  • 前段时间和同事一起买刮刮乐,同事们或多或少都中过,有的运气王竟然每次都会赚点钱,我从来没中过,于是我感觉刮刮乐不适合我,开始寻思买双色球,粗略算了下双色球中的概率,心累了,看着自己写的代码跑来跑去,投入的低的话还是赔钱的概率大(投入高的话有惊喜),算了,没有赚大奖的命,老老实实搬砖吧。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * @description:
 * @author: dengju
 * @create: 2019-10-28 16:57
 **/
public class TestCai {

    public static void main(String[] args) throws Exception {
        //选择红球
        ArrayList<String> redBalls = chooseRedBalls(5, 8, 14, 13, 31, 7);
        //选择蓝球
        String blueBall = chooseBlueBall(15);
        //自选5000次
        zixuan(redBalls, blueBall, 5000);


//        //机选5000次
//        jixuan(5000);

    }

    /**
     * 选择红球
     *
     * @param redBalls
     * @return
     * @throws Exception
     */
    public static ArrayList<String> chooseRedBalls(Integer... redBalls) throws Exception {
        ArrayList<String> result = new ArrayList<>();
        if (redBalls.length != 6) {
            throw new Exception("请选择6个红球!");
        }
        for (Integer redBall : redBalls) {
            if (redBall < 1 || redBall > 33) {
                throw new Exception("请选择介于1到33的红球!");
            } else if (result.contains(redBall + "")) {
                throw new Exception("选择的6个红球号码不能重复!");
            } else {
                result.add(redBall + "");
            }
        }
        return result;
    }

    /**
     * 选择蓝球
     *
     * @param blueBall
     * @return
     * @throws Exception
     */
    public static String chooseBlueBall(Integer blueBall) throws Exception {
        if (blueBall < 1 || blueBall > 16) {
            throw new Exception("请选择介于1到16的蓝球");
        }
        return blueBall + "";
    }


    /**
     * 自选
     *
     * @param redBalls
     * @param blueBall
     * @param note
     */
    private static void zixuan(ArrayList<String> redBalls, String blueBall,/*彩票注数*/Integer note) {
        Long count = 0L;
        Long money = 0L;
        while (true) {
            count++;
            HashMap<String, Object> result1 = start();
            ArrayList<String> reds1 = (ArrayList<String>) result1.get("red");
            String blue1 = (String) result1.get("blue");
            if (blue1.equalsIgnoreCase(blueBall)) {
                Integer compare = compare(reds1, redBalls);
                System.out.print("中了" + compare + "个红球,蓝球中了!");
                switch (compare) {
                    case 0:
                    case 1:
                    case 2:
                        money += 5;
                        System.out.println("中了5元");
                        break;
                    case 3:
                        money += 10;
                        System.out.println("中了10元");
                        break;
                    case 4:
                        money += 200;
                        System.out.println("中了200元");
                        break;
                    case 5:
                        money += 3000;
                        System.out.println("中了3000元");
                        break;
                    case 6:
                        money += 10000000;
                        System.out.println("中了一千万元");
                        break;

                }

                if (compare == 6) {
                    System.err.println("全中!!!!!!!!!!用了" + count + "次");
                    System.out.println("共消费" + (count * 2) + "元钱,中了" + money + "元钱!");
                    break;
                }
            } else {
                Integer compare = compare(reds1, redBalls);
                System.out.print("中了" + compare + "个红球,蓝球没中!");
                switch (compare) {
                    case 4:
                        money += 10;
                        System.out.println("中了10元");
                        break;
                    case 5:
                        money += 200;
                        System.out.println("中了200元");
                        break;
                    case 6:
                        money += 5000000;
                        System.out.println("中了五百万元");
                        break;
                    default:
                        System.out.println();
                }
            }
            //总共买进次数
            if (count.intValue() == note) {
                System.out.println("共消费" + (count * 2) + "元钱,中了" + money + "元钱!");
                break;
            }
        }

    }

    /**
     * 机选
     *
     * @param note
     */
    private static void jixuan(/*彩票注数*/Integer note) {
        Long count = 0L;
        Long money = 0L;
        while (true) {
            count++;
            HashMap<String, Object> result1 = start();
            HashMap<String, Object> result2 = start();
            ArrayList<String> reds1 = (ArrayList<String>) result1.get("red");
            String blue1 = (String) result1.get("blue");
            ArrayList<String> reds2 = (ArrayList<String>) result2.get("red");
            String blue2 = (String) result2.get("blue");
            if (blue1.equalsIgnoreCase(blue2)) {
                Integer compare = compare(reds1, reds2);
                System.out.print("中了" + compare + "个红球,蓝球中了!");
                switch (compare) {
                    case 0:
                    case 1:
                    case 2:
                        money += 5;
                        System.out.println("中了5元");
                        break;
                    case 3:
                        money += 10;
                        System.out.println("中了10元");
                        break;
                    case 4:
                        money += 200;
                        System.out.println("中了200元");
                        break;
                    case 5:
                        money += 3000;
                        System.out.println("中了3000元");
                        break;
                    case 6:
                        money += 10000000;
                        System.out.println("中了一千万元");
                        break;

                }

                if (compare == 6) {
                    System.err.println("全中!!!!!!!!!!用了" + count + "次");
                    System.out.println("共消费" + (count * 2) + "元钱,中了" + money + "元钱!");
                    break;
                }
            } else {
                Integer compare = compare(reds1, reds2);
                System.out.print("中了" + compare + "个红球,蓝球没中!");
                switch (compare) {
                    case 4:
                        money += 10;
                        System.out.println("中了10元");
                        break;
                    case 5:
                        money += 200;
                        System.out.println("中了200元");
                        break;
                    case 6:
                        money += 5000000;
                        System.out.println("中了五百万元");
                        break;
                    default:
                        System.out.println();
                }
            }
            //总共买进次数
            if (count.intValue() == note) {
                System.out.println("共消费" + (count * 2) + "元钱,中了" + money + "元钱!");
                break;
            }
        }

    }


    private static Integer compare(List<String> list1, List<String> list2) {
        Integer result = 0;
        for (String s1 : list1) {
            for (String s2 : list2) {
                if (s1.equals(s2)) {
                    result++;
                    continue;
                }
            }
        }
        return result;
    }


    public static HashMap<String, Object> start() {
        ArrayList<String> redPool = getRedPool();
        ArrayList<String> bluePool = getBluePool();
        Random random = new Random();
        ArrayList<String> reds = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            String now = redPool.get(random.nextInt(redPool.size()));
            reds.add(now);
            redPool.remove("" + now);
        }
        String blue = bluePool.get(random.nextInt(16));

        System.out.println("红球为:" + reds + ",蓝球为:" + blue);

        HashMap<String, Object> result = new HashMap<>();
        result.put("red", reds);
        result.put("blue", blue);
        return result;
    }

    private static ArrayList<String> getBluePool() {
        ArrayList<String> bluePool = new ArrayList<>();
        for (int i = 1; i <= 16; i++) {
            bluePool.add(i + "");
        }
        return bluePool;
    }

    private static ArrayList<String> getRedPool() {
        ArrayList<String> redPool = new ArrayList<>();
        for (int i = 1; i <= 33; i++) {
            redPool.add(i + "");
        }
        return redPool;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值