本文主要是介绍C#用数组实现控制台进行体育彩票玩法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.代码(主函数体)
static void Main(string[] args){//LotteryMaker();//BuyLottery();//int []a1 = { 1, 2, 3,4,5,6,7 };//int []a2 = { 1, 2, 3,4,5,6,7 };//WinLevel(a1,a2);//Console.WriteLine(Award(2));Console.WriteLine(Award(WinLevel(BuyLottery(), LotteryMaker())));}/// <summary>/// 生成中奖的彩票(返回数组Lottery)/// </summary>/// <returns></returns>private static int[] LotteryMaker(){Random random = new Random();int[] Lottery = new int[7];for (int i = 0; i < 6;){int RedNumber = random.Next(1, 34);if (Array.IndexOf(Lottery, RedNumber) < 0){Lottery[i] = RedNumber;i++;}else continue;}int BlueNumber = random.Next(1, 17);Lottery[6] = BlueNumber;int k, j,temp;for (k = 0; k < 7 - 1; k++){//每一轮比较前 n-1-i 个,也就是说,已经排序好的最后 i 个不用比较for (j = 0; j < 7 - 1 - k; j++){if (Lottery[j] > Lottery[j + 1]){temp = Lottery[j];Lottery[j] = Lottery[j + 1];Lottery[j + 1] = temp;}}}//输出排序后的数组Console.WriteLine();Console.WriteLine("中奖彩票为:");for (k = 0; k < 7; k++){Console.Write(Lottery[k]+",");}Console.WriteLine();Console.WriteLine("您获得了一个:");return Lottery;}/// <summary>/// 用户购买彩票(返回数组BuyNumber)/// </summary>/// <returns></returns>private static int[] BuyLottery(){int[] BuyNumber = new int[7];for (int i = 0; i < 6;){Console.WriteLine("请输入第{0}个红球的购买数字:",i+1);int Red = int.Parse(Console.ReadLine());if (Red >= 1 && Red <= 33 ){if (Array.IndexOf(BuyNumber, Red) < 0) {BuyNumber[i] = Red;i++; }else Console.WriteLine("已经购买过了,请换个号码");}else{Console.WriteLine("输入范围越界,请输入红球1-33的数字");continue;} }Console.WriteLine("请输入第7个蓝球的购买数字:");int Blue;for (int i = 0; i<1 ;){Blue = int.Parse(Console.ReadLine());if (0 < Blue && Blue < 17) { BuyNumber[6] = Blue; i++; }else{Console.WriteLine("请输入篮球1-16的数字");continue;}}Console.WriteLine("你买的彩票是:");for (int i = 0; i < 7; i++)Console.Write(BuyNumber[i]+",");return BuyNumber;}/// <summary>/// 彩票的中奖等级(返回int level)/// </summary>/// <param name="BuyNumber"></param>/// <param name="Lottery"></param>/// <returns>Level</returns>private static int WinLevel(int []BuyNumber,int []Lottery){int LevelBlue=0,LevelRed=0,Level = 0;for (int i = 0; i < 6; i++){if (Array.IndexOf(Lottery, BuyNumber[i]) >= 0) LevelRed++;}if (BuyNumber[6] == Lottery[6]){LevelBlue++;}if (LevelBlue == 0){switch (LevelRed){case 0: Level=0; break;case 1: Level = 1; break;case 2: Level = 2; break;case 3: Level = 3; break;case 4: Level = 4; break;case 5: Level = 5; break;case 6: Level = 6; break;}}else{switch (LevelRed){case 0: Level = 1;break;case 1: Level = 2; break;case 2: Level = 3; break;case 3: Level = 4; break;case 4: Level = 5; break;case 5: Level = 6; break;case 6: Level = 7; break;}}return Level;}/// <summary>/// 输出获奖信息(返回string类型award)/// </summary>/// <param name="Level"></param>/// <returns></returns>private static string Award(int Level){string award = null;//对award进行赋初始值switch (Level){case 0: award = "给我100元的机会";break;case 1: award = "参与奖五块钱"; break;case 2: award = "参与奖十块钱"; break;case 3: award = "三等奖100元"; break;case 4: award = "二等奖500元"; break;case 5: award = "一等奖1000元"; break;case 6: award = "特等奖5000元"; break;case 7: award = "超级大奖一千万"; break;}return award;}
2.输出结果
输出结果包含所有情况(除了中奖情况= =,我真就是中奖绝缘体哈哈哈)
3.原理
3.1体彩要求
该体彩6个红球数值为1-33且互不相同,1个蓝球数值为1-16;并将中奖号码从小到大输出。
中奖条件是user买的彩票号码中与实际中奖号码中,红球或蓝球存在相同即可(蓝球为最后一个数)
中奖等级分为0个蓝+x个红;1个蓝+x个红,共8个等级(1<= x<= 6)
3.2分配奖励
用swich-case针对奖励的进行分配放置奖励 。记得在最前面对award进行赋初始值来避免后面return出现的“无赋值”的情况。
3.3获取获奖等级
通过Array.IndexOf函数获取在Lottery这个数组中是否存在BuyNumber数组中的数的索引,以此来检测是否有数据的对应
3.4购买彩票
依旧是通过Array.IndexOf函数获取数组中是否已经存在购买过的号数
3.5彩票中奖号码制作
3.6主函数
通过每个函数返回的值以及需要输入的值放置出一大行,分开写与注释所展示的代码类似。
这篇关于C#用数组实现控制台进行体育彩票玩法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!