//判断中奖情况 public void zhongjiangqingkuang(int num) { if(num>=4) { Console.Write("恭喜您中奖了"); if (num == 6) { Console.Write("特等奖,百万奖金带回家"); } if (num == 5) { Console.Write("一等奖,万元奖金带回家"); } else if (num == 4) { Console.Write("千元奖金带回家"); } } else { Console.Write("谢谢惠顾,欢迎再来"); } }
public int[] Shuru() { int[] haoma=new int[7]; try { Console.WriteLine("输入您要选择的红球号码:"); for (int i = 0; i < 6; i++) { Console.Write("第"+(i+1)+"个球数为:"); haoma[i] = int.Parse(Console.ReadLine()); } Console.Write("输入您要选择的蓝球号码:"); int a = int.Parse(Console.ReadLine()); haoma[6] = a; } catch (Exception ex) { Console.WriteLine("有错误,错误内容是" + ex.Message); } return haoma; }
//输出球号
public void shuchu(int[]shuzu)
{
for (int i = 0; i < shuzu.Length;i++ )
{
Console.Write(shuzu[i]+" ");
}
}
//获取相同球号数目 public int Panduan(int[] shuzu1,int[] shuzu2,int[] shuzu3) { int num=0; if(shuzu1[shuzu1.Length-1]==shuzu3[shuzu3.Length-1]) { num++; } for (int i = 0; i < shuzu1.Length-1;i++ ) { for (int j = 0; j < shuzu2.Length;j++ ) { if (shuzu2[j] == shuzu1[i]) { num++; } } } return num; }
//随机产生互不相同的球号 public int[] getRandomNum(int num, int minValue, int maxValue) { Random ra = new Random(unchecked((int)DateTime.Now.Ticks)); int[] arrNum = new int[num]; int tmp = 0; for (int i = 0; i <= num - 1; i++) { tmp = ra.Next(minValue, maxValue); //随机取数 arrNum[i] = getNum(arrNum, tmp, minValue, maxValue, ra); //取出值赋到数组中 } //int b = ra.Next(16); //arrNum[num - 1] = b; return arrNum; } public int getNum(int[] arrNum, int tmp, int minValue, int maxValue, Random ra) { int n = 0; while (n <= arrNum.Length - 1) { if (arrNum[n] == tmp) //利用循环判断是否有重复 { tmp = ra.Next(minValue, maxValue); //重新随机获取。 getNum(arrNum, tmp, minValue, maxValue, ra);//递归:如果取出来的数字和已取得的数字有重复就重新随机获取。 } n++; } return tmp; } }
Shuangseqiu shu = new Shuangseqiu(); //获取输入号码 int[] qiuhao= shu.Shuru(); Console.Write("您的号码为:"); shu.shuchu(qiuhao); //产生红色中奖号码 int[] redarr= shu.getRandomNum(6,1,33); Console.WriteLine("红色中奖号码为:"); shu.shuchu(redarr); //产生蓝色中奖号码 int[] bluearr = shu.getRandomNum(1, 1, 16); Console.WriteLine("蓝色中奖号码为:"); shu.shuchu(bluearr); //int[] zhongjiangNum = shu.zhongjianghao(redarr,bluearr); //Console.WriteLine("中奖号码为:"); //shu.shuchu(zhongjiangNum); //比较相等个数 int no=shu.Panduan(qiuhao,redarr,bluearr); Console.Write("相同的数为:"+no); shu.zhongjiangqingkuang(no); Console.ReadLine();
时间: 2024-10-06 22:52:59