AI智能
改变未来

c#控制台程序 模拟买彩票

可以选择系统随机的7个号码或者自己输入7位数,与系统的随机数对比,运行一次或者运行直到中一等奖(显示花费)。
啊,再也不买彩票了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lotteryticket
{
class Program
{
static Random random = new Random();
static void Main(string[] args)
{
Console.WriteLine(“请选择对应操作的序号”);
Console.WriteLine(“1、系统随机7位号码”);
Console.WriteLine(“2、手动输入7位号码”);
int[] MNum = new int[7];
int[] UNum = new int[7];
int[] IfWin = new int[2];
int OneChose = int.Parse(Console.ReadLine());
if (OneChose == 1)//系统生成
{
MNum = GenerateNumber();
Console.WriteLine(“你的彩票号码是:”);
foreach (int item in MNum)
{
Console.Write(item+ \” \”);
}
}
else if (OneChose == 2)//手动输入
{
for (int i = 0; i < 6; i++)//输入红球号
{
Console.WriteLine(“请输入红球第{0}个号码(1~33内不重复的数字)”, i + 1);
int OneNum = int.Parse(Console.ReadLine());
if (OneNum < 1 || OneNum > 33)
{
Console.WriteLine(“超出范围,请重新输入!”);
i–;
}
else if(Array.IndexOf(MNum, OneNum)>=0)
{
Console.WriteLine(“数字重复,请重新输入!”);
i–;
}
else MNum[i] = OneNum;
}
for (int i = 0; i < 1; i++)//输入蓝球号
{
Console.WriteLine(“请输入蓝球的号码(1~16)”);
int TwoNum = int.Parse(Console.ReadLine());
if (TwoNum < 1 || TwoNum > 16)
{
Console.WriteLine(“超出范围,请重新输入!”);
i–;
}
else MNum[6] = TwoNum;
Console.WriteLine(“你的彩票号码是:”);
foreach (int item in MNum)
{
Console.Write(item + \” \”);
}
}
}
Console.WriteLine(“请选择对应操作的序号”);
Console.WriteLine(“1、只一次”);
Console.WriteLine(“2、重复直到一等奖”);
int TwoChose = int.Parse(Console.ReadLine());
if (TwoChose ==1)//只运行一次
{
UNum = GenerateNumber();
Console.WriteLine(“中奖号码是:”);
foreach (int item in UNum)
{
Console.Write(item + \” \”);
}
IfWin = Comparison(UNum, MNum);
int level = BonusLevel(IfWin);
if (level == 1) Console.WriteLine(“中了一等奖!”);
else if (level == 2) Console.WriteLine(“中了二等奖!”);
else if (level == 3) Console.WriteLine(“中了三等奖!”);
else if (level == 4) Console.WriteLine(“中了四等奖!”);
else if (level == 5) Console.WriteLine(“中了五等奖!”);
else if (level == 6) Console.WriteLine(“中了六等奖!”);
else if (level == 7) Console.WriteLine(“没有中奖”);
else Console.WriteLine(“系统错误!”);
}
else//运行直到中一等奖
{
int i = 0;
int level=0;
do
{
UNum = GenerateNumber();
Console.WriteLine(“中奖号码是:”);
foreach (int item in UNum)
{
Console.Write(item + \” \”);
}
IfWin = Comparison(UNum, MNum);
level = BonusLevel(IfWin);
i++;
if (level == 1) Console.WriteLine(“中了一等奖!花费{0}元”, 2 * i);
else if (level == 2) Console.WriteLine(“中了二等奖!花费{0}元”, 2 * i);
else if (level == 3) Console.WriteLine(“中了三等奖!花费{0}元”, 2 * i);
else if (level == 4) Console.WriteLine(“中了四等奖!花费{0}元”, 2 * i);
else if (level == 5) Console.WriteLine(“中了五等奖!花费{0}元”, 2 * i);
else if (level == 6) Console.WriteLine(“中了六等奖!花费{0}元”, 2 * i);
else if (level == 7) Console.WriteLine(“没有中奖,花费{0}元”, 2 * i);
else Console.WriteLine(“系统错误!”);
} while (level!= 1);
}
Console.ReadLine();
}
private static int[] GenerateNumber()//生成7位号码
{
int[] UNum = new int[7];
for (int i=0;i<=5;i++)//红球
{
int OneNum=random.Next(1, 34);//在1——33中随机产生一个数
if (Array.IndexOf(UNum, OneNum)>=0) i–;//存在,重新生成一个号码
else UNum[i] = OneNum ;

}int TwoNum = random.Next(1, 17);//在1——16中随机产生一个数UNum[6] = TwoNum;return UNum;}private  static int[] Comparison(int[] UNum,int[]MNum)//对比两个数组{int[] IfWin = new int[2];int num1 = 0;int num2 = 0;for(int i=0;i<6;i++){if (Array.IndexOf(UNum, MNum[i])>=0) num1++;}if (UNum[6] == MNum[6]) num2++;IfWin[0] = num1;IfWin[1] = num2;return IfWin;}private  static int BonusLevel(int[] IfWin)//奖励等级{int level;if (IfWin[0] == 6 && IfWin[1] == 1) level = 1;else if (IfWin[0] == 6 && IfWin[1] == 0) level = 2;else if (IfWin[0] == 5 && IfWin[1] == 1) level = 3;else if ((IfWin[0] == 5 && IfWin[1] == 0) || (IfWin[0] == 4 && IfWin[1] == 1)) level = 4;else if ((IfWin[0] == 4 && IfWin[1] == 0) || (IfWin[0] == 3 && IfWin[1] == 1)) level = 5;else if (IfWin[0] == 2 && IfWin[1] == 1) level = 6;else level = 7;return level;}}

}

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » c#控制台程序 模拟买彩票