用java写一个双色球的彩票程序(源码)

尝试模拟双色球的java程序(源码)

简介

作为一个打工人,每天都非常合理的想着如何暴富,曾经跟hxd闲聊的时候说,每个月自己除了理财之外,还要拿出一部分钱(5%左右)购买彩票,中了明天辞职,没中就当做做公益,也许是因为幸存者偏差,让我这个史诗非酋总有一种迟早有翻身之日的错觉,某日摸鱼便想着自己写写,看看中奖到底靠不靠谱,于是便有了这篇文章。

双色球具体规则是借鉴了网上的,奖池大小没办法模拟,奖金分配也不是很恰当,总体来说就是毛病多多,只是一个娱乐版本,有兴趣的朋友提出建议我会积极采纳修改,就酱~

P.S.写到现在跑过不少次,但是从来没有中过一等奖,每一期买五十万张啊。。。二等奖中过十来次,每一期投入5000W,平均收益5W元左右。自己给自己重锤,这年头像我这么自律的人估计8多了(狗头)

双色球玩法

源码

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @author : rjj
 * @datetime : 2021-06-09 09:47
 **/
public class TicketRun {
    public static String[] rightNum;
    public static BigDecimal totalMoney;
    public static int totalCount;

    public static void main(String[] args) {
        System.out.println("欢迎光临彩票店\n");
        menu();
        System.out.println("谢谢惠顾");
    }

    static void menu() {
        Scanner scanner = new Scanner(System.in);
        String[] menu = new String[]{"1", "2", "3", "4"};
        System.out.println(
                "①键入1开始机选下注\n" +
                        "②键入2下注测试\n" +
                        "③键入3离开彩票店\n"
        );
        String option = scanner.next();

        if (Arrays.asList(menu).contains(option)) {
            switch (option) {
                case "4":
                    return;
                case "1":
                    machineSelectedNum();
                    break;
                case "2":
                    manuallySelectedNum();
                    break;
                default:
            }
        } else {
            System.out.println("输入错误,重新输入");
            menu();
        }
    }

    static void machineSelectedNum() {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Ticket> tickets = new ArrayList<>();
        int count = 0;
        flag:
        while (true) {
            String[] myTicket = generateTicket();
            System.out.print("机选结果为:" + Arrays.toString(myTicket) + ", 请输入您需要下几注(每注2元)?键入0重新选号。");
            count = scanner.nextInt();
            int money = count * 2;
            if (money == 0) {
                continue;
            } else {
                Ticket ticket = new Ticket(myTicket, money);
                tickets.add(ticket);
                count++;
            }
            if (count == 1000000) {
                break;
            }
            while (true) {
                System.out.println("继续选号吗?(Y/N)");
                String option = scanner.next();
                if ("Y".equalsIgnoreCase(option)) {
                    continue flag;
                } else if ("N".equalsIgnoreCase(option)) {
                    break flag;
                } else {
                    System.out.println("输入错误");
                }
            }

        }
        rightNum = generateTicket();
        totalMoney = BigDecimal.valueOf(new SecureRandom().nextInt(500) + 1).multiply(BigDecimal.valueOf(1000000L));
        totalCount = new SecureRandom().nextInt(20) + 1;
        ArrayList<Result> results = computed(tickets);
        System.out.println("本期奖池 " + totalMoney + " 元, 您共购买了 " + results.size() + " 张彩票,共计花费" + count * 2 + " 元" + "本期中奖号码为:" + Arrays.toString(rightNum));
        System.out.println("您的下注情况如下:");
        int index = 0;
        for (Result result : results) {
            System.out.println(
                    (index + 1) +
                            "、" +
                            Arrays.toString(result.ticket.ticket) +
                            "共下 " +
                            result.ticket.money / 2 +
                            " 注;①红球命中: " +
                            result.rightNum[0] +
                            " 个, ②蓝球命中: " +
                            result.rightNum[1] +
                            "个, " +
                            result.winningSituation +
                            ",奖金: " +
                            result.money +
                            "元");
            index++;
        }
        menu();
    }

    static void manuallySelectedNum() {
        Scanner scanner = new Scanner(System.in);
        SecureRandom random = new SecureRandom();

        int index = 0;
        int num = 0;
        int num1 = 0;

        System.out.println("下注测试开始,自定下注选1,电脑下注(50w张,每张50注)选2");
        int option = scanner.nextInt();

        if (option == 2) {
            num = 500000;
            num1 = 50;
        } else if (option == 1) {
            System.out.println("下注测试开始,请输入你要购买的彩票数量");
            num = scanner.nextInt();
            System.out.println("请输入每张彩票需要下多少注?");
            num1 = scanner.nextInt();
        } else {
            System.out.println("输入错误!重新输入");
            manuallySelectedNum();
        }
        System.out.println("\n\n======= ======= ======= ======= ======= 开始测试 ======= ======= ======= ======= =======\n\n");

        int time = 1;
        boolean isFlag = true;
        while (isFlag) {
            int count = 0;
            ArrayList<Ticket> tickets = new ArrayList<>();
            BigDecimal testMoney = BigDecimal.ZERO;
            BigDecimal max = BigDecimal.ZERO;
            totalMoney = BigDecimal.valueOf(random.nextInt(500) + 1).multiply(BigDecimal.valueOf(1000000L));
            totalCount = random.nextInt(20) + 1;

            while (true) {
                tickets.add(new Ticket(generateTicket(), num1 * 2));
                count++;
                if (count == num) {
                    break;
                }
            }
            rightNum = generateTicket();
            ArrayList<Result> results = computed(tickets);

            System.out.println("\n");
            System.out.println("第" + time + "期奖池 " + totalMoney + " 元," + "本期中奖号码为:" + Arrays.toString(rightNum) + "中奖情况如下:");
            for (Result result : results) {
                if (result.money.compareTo(new BigDecimal("0")) > 0) {
                    testMoney = testMoney.add(result.money);
                    if ((result.money).compareTo(max) > 0) {
                        max = result.money;
                    }
                }
                if (result.money.compareTo(new BigDecimal("3000")) > 0) {
                    System.out.println(
                            (index + 1) +
                                    "、" +
                                    Arrays.toString(result.ticket.ticket) +
                                    "共下 " +
                                    result.ticket.money / 2 +
                                    " 注;①红球命中: " +
                                    result.rightNum[0] +
                                    " 个, ②蓝球命中: " +
                                    result.rightNum[1] +
                                    "个, " +
                                    result.winningSituation +
                                    ",奖金: " +
                                    result.money +
                                    "元");
                    if ("一等奖".equals(result.winningSituation)) {
                        int i = scanner.nextInt();
                        System.out.println(i);
                    }
                }
                index++;
            }
            System.out.println("您本次购买的" + count + "张彩票,共计花费 " + num * num1 * 2 + " 元,累计获得奖金 " + testMoney + " 元,最多获得奖金" + max + "元");
            time++;
        }

    }

    static String[] generateTicket() {
        SecureRandom random = new SecureRandom();
        String[] result = new String[7];
        for (int i = 0; i < result.length; i++) {
            if (i < result.length - 1) {
                String num = String.valueOf(random.nextInt(15) + 1);
                if (new Integer(num) <= 9) {
                    num = "0" + num;
                }
                result[i] = num;
            } else {
                String num = String.valueOf(random.nextInt(32) + 1);
                if (new Integer(num) <= 9) {
                    num = "0" + num;
                }
                result[i] = num;

            }
        }
        return result;
    }

    static ArrayList<Result> computed(List<Ticket> tickets) {
        ArrayList<Result> results = new ArrayList<>();
        for (Ticket ticket : tickets) {
            int red = 0;
            int blue = 0;
            int[] rightNum = new int[2];
            for (int i = 0; i < ticket.ticket.length; i++) {
                if (i == 6) {
                    if (ticket.ticket[i].equals(TicketRun.rightNum[i])) {
                        blue += 1;
                    }
                } else {
                    if (ticket.ticket[i].equals(TicketRun.rightNum[i])) {
                        red += 1;
                    }
                }
            }

            rightNum[0] = red;
            rightNum[1] = blue;

            results.add(new Result(ticket, rightNum, new BigDecimal(computedMoney(red, blue, ticket.money / 2)[0].toString()), computedMoney(red, blue, ticket.money / 2)[1]));

        }

        return results;

    }

    static String[] computedMoney(int red, int blue, int count) {

        String[] result = new String[2];
        if (red == 6 && blue == 1) {
            result[0] = totalMoney.multiply(BigDecimal.valueOf(0.55)).divide(BigDecimal.valueOf(count)).compareTo(BigDecimal.valueOf(500000000)) > 0
                    ?
                    String.valueOf((5000000L * count))
                    :
                    totalMoney.multiply(BigDecimal.valueOf(0.55)).toString();
            result[1] = "一等奖";
        } else if (red == 6 && blue == 0) {
            result[0] = totalMoney.multiply(BigDecimal.valueOf(0.25)).toString();
            result[1] = "二等奖";
        } else if (red >= 5 && blue == 1) {
            result[0] = "3000";
            result[1] = "三等奖";
        } else if ((red == 5 && blue == 0) || (red == 4 && blue == 1)) {
            result[0] = "200";
            result[1] = "四等奖";
        } else if ((red == 4 && blue == 0) || (red == 3 && blue == 1)) {
            result[0] = "10";
            result[1] = "五等奖";
        } else if (red == 0 && blue == 1) {
            result[0] = "5";
            result[1] = "六等奖";
        } else {
            result[0] = "0";
            result[1] = "未中奖";
        }

        return result;
    }

}

class Ticket {
    String[] ticket;
    int money;

    public Ticket(String[] ticket, int money) {
        this.ticket = ticket;
        this.money = money;
    }
}

class Result {
    public Ticket ticket;
    public int[] rightNum;
    public BigDecimal money;
    public String winningSituation;

    public Result(Ticket ticket, int[] rightNum, BigDecimal money, String winningSituation) {
        this.ticket = ticket;
        this.rightNum = rightNum;
        this.money = money;
        this.winningSituation = winningSituation;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

歌诗图er

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

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

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

打赏作者

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

抵扣说明:

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

余额充值