C#综合训练—幸运抽奖
2019-01-03 本文已影响0人
八度_八度
要求:根据要求写代码
代码:
namespace 任务1
{
class Program
{
static void Main(string[] args)
{
string flag =""; //用于标识用户是否继续
string userName = ""; //用户名
string password = ""; //密码
string cardNum = ""; //会员卡号
bool isReg = false;
bool isLogin = false;
//获得随机数
Random rd = new Random();
int max = 9999;
int min = 1000;
do{
Console.WriteLine(@"
*************欢迎进入魔法师幸运抽奖系统*************
1、注册
2、登录
3、抽奖
*************************************************
");
Console.Write("请选择菜单:");
string str_menu_num = Console.ReadLine();
switch (str_menu_num)
{
case "1":
Console.WriteLine("魔法师幸运抽奖系统 > 注册");
string [] result = Reg();
if (string.IsNullOrEmpty(result[0]))
{
isReg = false;
}
else
{
userName = result[0];
password = result[1];
cardNum = result[2];
isReg = true;
}
break;
case "2":
if (isReg)
{
Console.WriteLine("魔法师幸运抽奖系统 > 登录");
for (int i = 1; i <= 3; i++)
{
Console.Write("第{0}/3次,请输入用户名:",i);
string un = Console.ReadLine();
Console.Write("第{0}/3次,请输入密码:",i);
string pw = Console.ReadLine();
if (userName == un && password == pw)
{
Console.WriteLine("欢迎您:{0}", userName);
isLogin = true;
break;
}
else
{
isLogin = false;
continue;
}
}
}
else
{
Console.WriteLine("您没有注册,请先注册后再登录");
}
break;
case "3":
if (isLogin)
{
Console.WriteLine("魔法师幸运抽奖系统 > 抽奖");
Console.Write("请输入会员卡号:");
string str_card_num = Console.ReadLine();
string[] lucknums = new string[5];
Console.Write("本日的幸运数字为:");
for (int i = 0; i < lucknums.Length; i++)
{
lucknums[i] = rd.Next(min, max).ToString();
Console.Write(lucknums[i] + "\t");
}
int index = Array.IndexOf(lucknums, str_card_num);
if (index == -1)
{
Console.WriteLine("抱歉,您不是本日的幸运会员");
}
else
{
Console.WriteLine("恭喜您,您中奖了了~~~");
}
}
else
{
Console.WriteLine("您没有登录,请先登录后再抽奖");
}
break;
default:
Console.WriteLine("您的输入有误~");
break;
}
Console.Write("继续吗?(y/n)");
flag = Console.ReadLine();
}while(flag == "y"||flag == "Y");
Console.WriteLine("系统退出,谢谢使用");
Console.ReadKey();
}
/**
* 注册
* */
static string[] Reg()
{
string[] ret = new string[3];
Random rd = new Random();
int max = 9999;
int min = 1000;
Console.WriteLine("请填写个人注册信息");
Console.Write("用户名:");
string userName = Console.ReadLine();//获得用户输入的用户名
if (userName == "admin")
{
Console.WriteLine("admin帐户已被占用");
return ret;
}
//Console.WriteLine();
Console.Write("密码:");
string password = Console.ReadLine(); //获得用户输入的密码
//用随机的数作为卡号
string cardNum = rd.Next(min, max).ToString();
Console.WriteLine("注册成功,请记好您的会员卡号");
Console.WriteLine("用户名\t密码\t会员卡号");
Console.WriteLine("{0}\t{1}\t{2}", userName, password, cardNum);
ret[0] = userName;
ret[1] = password;
ret[2] = cardNum;
return ret;
}
}
}