首先声明我是新手,下面代码肯定有很多不足的地方,我是学一点就回过头来补充一点。
下面这段代码,我不满意的地方是:
当我输入1 2 3其中任意一个时,运行提示:请输入密码。然后我输入错误密码,提示:密码错误。程序返回到请输入姓名代号,这时候问题就出来了,当我输入一个非数字时就提示:对不起输入错误
请输入密码
我想要的是,当我输入非数字时应该提示:对不起输入错误
请输入你的姓名代号应该怎么改改!bool mybool = false;
string mypassword;
int myid = 0;
do
{
Console.WriteLine("请输入你的姓名代号:");
Console.WriteLine("(1)张三,(2)李四,(3)王五");
try
{
myid = Convert.ToInt32(Console.ReadLine().Trim());
}
catch (Exception e)
{
Console.WriteLine("对不起输入错误");
}
switch (myid)
{
case 1: Console.WriteLine("请输入你的密码:");
mypassword = Console.ReadLine().Trim();
if (mypassword == "zhang")
{
Console.WriteLine("密码正确:");
mybool = true;
}
else
{
Console.WriteLine("密码错误!");
}
break;
case 2:
Console.WriteLine("请输入你的密码:");
mypassword = Console.ReadLine().Trim();
if (mypassword == "li")
{
Console.WriteLine("密码正确:");
mybool = true;
}
else
{
Console.WriteLine("密码错误!");
}
break;
case 3:
Console.WriteLine("请输入你的密码:");
mypassword = Console.ReadLine().Trim();
if (mypassword == "wang")
{
Console.WriteLine("密码正确:");
mybool = true;
}
else
{
Console.WriteLine("密码错误!");
}
break;
default:
Console.WriteLine("查无此人!");
break;
}
} while (!mybool);
Console.WriteLine("谢谢使用,系统退出.......\n按回车键退出!");
Console.ReadLine();
下面这段代码,我不满意的地方是:
当我输入1 2 3其中任意一个时,运行提示:请输入密码。然后我输入错误密码,提示:密码错误。程序返回到请输入姓名代号,这时候问题就出来了,当我输入一个非数字时就提示:对不起输入错误
请输入密码
我想要的是,当我输入非数字时应该提示:对不起输入错误
请输入你的姓名代号应该怎么改改!bool mybool = false;
string mypassword;
int myid = 0;
do
{
Console.WriteLine("请输入你的姓名代号:");
Console.WriteLine("(1)张三,(2)李四,(3)王五");
try
{
myid = Convert.ToInt32(Console.ReadLine().Trim());
}
catch (Exception e)
{
Console.WriteLine("对不起输入错误");
}
switch (myid)
{
case 1: Console.WriteLine("请输入你的密码:");
mypassword = Console.ReadLine().Trim();
if (mypassword == "zhang")
{
Console.WriteLine("密码正确:");
mybool = true;
}
else
{
Console.WriteLine("密码错误!");
}
break;
case 2:
Console.WriteLine("请输入你的密码:");
mypassword = Console.ReadLine().Trim();
if (mypassword == "li")
{
Console.WriteLine("密码正确:");
mybool = true;
}
else
{
Console.WriteLine("密码错误!");
}
break;
case 3:
Console.WriteLine("请输入你的密码:");
mypassword = Console.ReadLine().Trim();
if (mypassword == "wang")
{
Console.WriteLine("密码正确:");
mybool = true;
}
else
{
Console.WriteLine("密码错误!");
}
break;
default:
Console.WriteLine("查无此人!");
break;
}
} while (!mybool);
Console.WriteLine("谢谢使用,系统退出.......\n按回车键退出!");
Console.ReadLine();
static void Main(string[] args)
{
//列出菜单
Console.WriteLine("请输入你的姓名代号:(输入Exit退出系统)");
Console.WriteLine("(1)张三,(2)李四,(3)王五"); //获取用户ID
int ID = GetID(); //检查密码
if (ID != -1)
CheckPassword(ID); Console.WriteLine("谢谢使用,系统退出.......\n按回车键退出!");
Console.ReadLine();
} //获取用户ID
static int GetID()
{
int ID;
while (true)
{
string input = Console.ReadLine().Trim();
if (input.Trim() == "Exit")
return -1;
bool ok = int.TryParse(input, out ID);
if (!ok || ID < 0 || ID > 3)
Console.WriteLine("对不起,您的输入不正确,请输入代号。(输入Exit退出系统)");
else
return ID;
}
} //根据ID检查密码,返回 bool 型
static bool CheckPassword(int ID)
{
for (int i=0;i<3; i++)
{
Console.WriteLine("请输入你的密码:");
string input = Console.ReadLine().Trim();
if ((ID == 1 && input == "zhang") || (ID == 2 && input == "li") || (ID == 3 && input == "wang"))
{
Console.WriteLine("密码正确。");
return true;
}
else
Console.WriteLine("密码错误。");
} return false;
}