C# 数组应用-双色球模拟

1 控制台买入彩票模拟

买彩票分为两步:

  • 一是买6个1-33的数字的红球,且6个数字不能重复,如果输入重复了提请重新输入;
  • 二是买入1个1-16数字的蓝球。
static int[] BuyLottery()
        {
            int[] LotteryArry = new int[7];
            for (int i = 0; i < LotteryArry.Length - 1;)
            {
                Console.WriteLine("请选择您的第{0}个红球号码:", i + 1);
                int RedBallNumber = int.Parse(Console.ReadLine());
                if (RedBallNumber < 1 || RedBallNumber > 33)
                    Console.WriteLine("您选的红球号码不在1-33,请重新选择:");
                //ndexof() :在字符串中从前向后定位字符和字符串;
                //所有的返回值都是指在字符串的绝对位置,如为空则为- 1
                else if (Array.IndexOf(LotteryArry, RedBallNumber) != -1)
                    Console.WriteLine("您选择的号码重复了,请重新输入:");
                else
                    LotteryArry[i++] = RedBallNumber;
            }


            int blueBallNumber;

            do
            {
                Console.WriteLine("请选择您蓝色球号码:");
                blueBallNumber = int.Parse(Console.ReadLine());
                if (blueBallNumber >= 1 && blueBallNumber <= 16)
                {
                    LotteryArry[6] = blueBallNumber;
                }
                else
                    Console.WriteLine("您选择的蓝色球号码必须是1-16,请重新选择");
            } while (blueBallNumber < 1 || blueBallNumber > 16);
            return LotteryArry;
        }

2 生成随机彩票

生成彩票分为两步:

  • 一是生成6个1-33的数字作为红球彩票,且6个数字不能重复;
  • 二是生成1个1-16数字作为的蓝球彩票。
 static Random ran = new Random();
 static int[]CreateLottery()
        {
            int[] CreateLottery = new int[7];

            for (int i = 0; i < CreateLottery.Length - 1;)
            {
                int num = ran.Next(1, 34);//Random….Next (minValue , maxValue): 返回一个指定范围内的随机数
                if (Array.IndexOf(CreateLottery, num) == -1)
                {
                    CreateLottery[i++] = num;
                }
            }
                CreateLottery[6] = ran.Next(1, 17);
                Array.Sort(CreateLottery, 0, 6);
                return CreateLottery;
            }

3 判断彩票的中奖级别

  • 一等奖:7个号码相符(6个红色球号码和1个蓝色球号码)(红色球号码顺序不限,下同)
  • 二等奖:6个红色球号码相符;
  • 三等奖:5个红色球号码和1个蓝色球号码相符;
  • 四等奖:5个红色球号码或4个红色球号码和1个蓝色球号码相符;
  • 五等奖:4个红色球号码或3个红色球号码和1个蓝色球号码相符;
  • 六等奖:1个蓝色球号码相符(有无红色球号码相符均可)。
        static int LotteryLevel(int[]buyLottery,int[] ranLottery)
        {
            int redCount = 0;int blueCount = 0;
            //红球中奖个数计算
            for (int i = 0; i < buyLottery.Length-1; i++)
            {
                if (Array.IndexOf(ranLottery, buyLottery[i], 0, 6) != -1)
                    redCount++;
            }
            //蓝球是否中奖判断
            blueCount = buyLottery[6] == ranLottery[6] ? 1 : 0;

            //中奖级别计算
            int level;
            if (redCount + blueCount == 7)
                level = 1;
            else if (redCount == 6)
                level = 2;
            else if (redCount + blueCount == 6)
                level = 3;
            else if (redCount + blueCount == 5)
                level = 4;
            else if (redCount + blueCount == 4)
                level = 5;
            else if (blueCount == 1)
                level = 6;
            else
                level = 0;
            return level;
        }

4 模拟中奖可能性

        static void PrintTicket(int[] ticket)
        {
            for (int i = 0; i < ticket.Length; i++)
            {
                Console.Write(ticket[i] + "\t");
            }
            Console.WriteLine();
        }
 static void Main(string[] args)
        {
            int[] myTicket = BuyLottery();
            Console.Clear();

            Console.WriteLine("我买的彩票:");

            PrintTicket(myTicket);
            Console.WriteLine("验证我的中奖潜力……");

            int count = 0;
            int level;
            do
            {
                count++;
                int[] ticket = CreateLottery();
                //购买彩票与开奖彩票对比
                level = LotteryLevel(myTicket, ticket);

                Console.WriteLine("*********开奖号码:*********");
                PrintTicket(ticket);

                if (level != 0)
                    Console.WriteLine("恭喜,{0}等奖。累计花费:{1}元", level, count * 2);
            } while (level != 1);
            Console.ReadKey();
        }
发布了19 篇原创文章 · 获赞 36 · 访问量 4083

猜你喜欢

转载自blog.csdn.net/CVSTO/article/details/104447417