| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1601 人关注过本帖
标题:写了个双色球模拟,花费¥5,000,000.00中了¥14,378,910.00,中奖次数:181 ...
只看楼主 加入收藏
bxe
Rank: 1
等 级:新手上路
帖 子:57
专家分:7
注 册:2019-3-21
结帖率:72.22%
收藏
 问题点数:0 回复次数:1 
写了个双色球模拟,花费¥5,000,000.00中了¥14,378,910.00,中奖次数:181438
程序代码:
        static Random random = new Random();

        private static void Main()
        {
            int choiceNumber = 1;
            for (; choiceNumber > 0 ; )
            {
                Console.WriteLine("双色球投注模拟,功能1:单注选择 2:购买机选 0:退出");
                Console.WriteLine("输入数字后回车,启动对应功能:");
                choiceNumber = int.Parse(Console.ReadLine());
                ChoiceFunction(choiceNumber);
            }

        }

        private static void Function02()
        {

            Console.WriteLine("输入要购买的注数:");
            int buynumber = int.Parse(Console.ReadLine());

            int prizeCount = 0, totalBonus = 0;
            int[] bonusNum = GenerateAGroupRandomNumber();
            for (int i = 0; i < buynumber; i++ )
            {
                int[] randomNumber = GenerateAGroupRandomNumber();
                for(int j = 0; j< randomNumber.Length; j++)
                {
                    Console.Write(randomNumber[j]+"\t");
                }
                Console.Write("\n\r");

                int level = OutputPrizeLevel(randomNumber, bonusNum);
                if (level > 0)
                {
                    prizeCount++;
                    totalBonus += BonusOfLevel(level);
                }
            }
            Console.WriteLine("花费{0:c}中了{1:c},中奖次数:{2}\n\r", buynumber * 2, totalBonus, prizeCount);
        }  


        private static void Function01()
        {
            //输入一注
            int[] buyNumber = InputNumber();
            //生成奖号
            int[] winNumber = GenerateAGroupRandomNumber();
            //判断是否中奖
            int level = OutputPrizeLevel(buyNumber, winNumber);
            //输出奖号
            Console.Write("中奖号码是:");
            for (int i = 0; i < 7; i++)
                Console.Write(winNumber[i] + ",");
            //输出结果
            if (level <= 0)
                Console.WriteLine("下次就能中了.");
            else
            {
                //判断奖金
                int bonus = BonusOfLevel(level);
                Console.WriteLine("恭喜!中了{0}等奖,奖金:{1}.", level, bonus);
            }
            Console.WriteLine("-------------- ");
        }

        /// <summary>
        /// 功能选择
        /// </summary>
        /// <param name="choiceNumber">编号</param>
        private static void ChoiceFunction(int choiceNumber)
        {
            if (choiceNumber == 1)
                Function01();
            if (choiceNumber == 2)
                Function02();
        }

        /// <summary>
        /// 判断并返回中奖等级
        /// </summary>
        /// <param name="buy">判断对象</param>
        /// <param name="winning">中奖号码</param>
        /// <returns></returns>
        private static int OutputPrizeLevel(int[] buy, int[] winning)
        {
            int redCount = 0, blueCount = 0;//分别计数
            for (int i = 0; i < 7; i++) //七位循环
            {
                if (i < 7 - 1)//红球比对
                {
                    int[] array = new int[6];
                    Array.Copy(winning, array, 6);//复制前六位
                    //检查是否有中奖号码
                    if (Array.IndexOf(winning, buy[i]) >= 0)
                        redCount += 1;
                   // Console.WriteLine("输出次数测试{0}", array[i]);
                }
                else //蓝球比对
                {
                    if (winning[i] == buy[i])
                        blueCount += 1;
                    // Console.WriteLine("输出次数测试{0}", i);
                }
            }
            int level = ResultLevelIs(redCount, blueCount);//中奖等级
            return level;
        }

        /// <summary>
        /// 红蓝球比对
        /// </summary>
        /// <param name="readCount"></param>
        /// <param name="blueCount"></param>
        /// <returns></returns>
        private static int ResultLevelIs(int readCount ,int blueCount)
        {
            if (readCount == 0 && blueCount == 0)
                return 0;
            else
            {
                if (readCount == 6 && blueCount == 1) return 1;
                else if (readCount == 6 && blueCount == 0) return 2;
                else if (readCount == 5 && blueCount == 1) return 3;
                else if (readCount == 5 && blueCount == 0) return 4;
                else if (readCount == 4 && blueCount == 1) return 4;
                else if (readCount == 4 && blueCount == 0) return 5;
                else if (readCount == 3 && blueCount == 1) return 5;
                else if (blueCount == 1) return 6;
                else return -1;
            }
        }

        /// <summary>
        /// 根据等级判断奖金
        /// </summary>
        /// <param name="level">中奖等级</param>
        /// <returns></returns>
        private static int BonusOfLevel(int level)
        {
            switch(level)
            {
                case 1: return 5000000;
                case 2: return 1250000;
                case 3: return 3000;
                case 4: return 200;
                case 5: return 10;
                case 6: return 5;
                default: return 0;
            }
        }

        /// <summary>
        /// 生成7位的随机数组
        /// </summary>
        /// <returns></returns>
        private static int[] GenerateAGroupRandomNumber()
        {
            int[] number = new int[7];
            for (int i = 0; i < 7; i++)
            {
                int randomNum = 0;
                if (i < 7 - 1)//前六位
                {
                    do
                    {
                        randomNum = random.Next(1, 33 + 1);
                    } while (Array.IndexOf(number, randomNum) >= 0);   //判断是否重复

                    number[i] = randomNum;
                }
                else//第七位
                {
                    number[i] = 99;//先将第七位赋值,方便排序
                    Array.Sort(number);
                    number[i] = random.Next(1, 16 + 1);
                }
            }
            return number;
        }

        /// <summary>
        /// 录入选择号码
        /// </summary>
        /// <returns></returns>
        private static int[] InputNumber()
        {
            int[] buyNum = new int[7];
            for (int i = 0; i < 7; i++)//提示输入
            {
                Console.WriteLine("第{0}个{1}球是: ", i + 1, (i == 6) ? "" : "");
                buyNum[i] = int.Parse(Console.ReadLine());

                if (i != 7 - 1)//判断前六位是否正确
                {
                    bool numberRepetition = false;
                    while (buyNum[i] > 33 || buyNum[i] < 1 || numberRepetition)
                    {
                        buyNum[i] = InputError();
                        numberRepetition = Array.IndexOf(buyNum, buyNum[i]) >= 0;
                    }
                }
                else//第七位判断
                {
                    while (buyNum[i] > 16 || buyNum[i] < 1)
                        buyNum[i] = InputError();
                }
            }
            return buyNum;
        }

        private static int InputError()
        {
            Console.WriteLine("输入有误,请重试!");
            return int.Parse(Console.ReadLine());
        }
第一次写200行代码,用的是C#语言,单注的功能没有发现问题,多注自动机选似乎中的太多了。。。
请老哥们看下,真没错的话我要去买彩票了。。。


[此贴子已经被作者于2019-7-10 16:17编辑过]

搜索更多相关主题的帖子: static int Console WriteLine return 
2019-07-10 16:12
bxe
Rank: 1
等 级:新手上路
帖 子:57
专家分:7
注 册:2019-3-21
收藏
得分:0 
似乎是开奖结果和随机购买用同一个随机种子造成的?

2019-07-10 16:48
快速回复:写了个双色球模拟,花费¥5,000,000.00中了¥14,378,910.00,中奖次数 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022823 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved