//定义原始密码
static int password = 123456;
//定义余额
static int yuanmoney = 2000;
/// <summary>
/// 定义存钱
/// </summary>
public static void cun()
{
Console.Write("请输入要存入的金额:");
int money = int.Parse(Console.ReadLine ());
//判断
if (money <=0)
{
Console.WriteLine("存入的金额不能为0或负数");
}
else
{
Console.WriteLine("存入金额"+money +"元");
yuanmoney = money + yuanmoney ;
Console.WriteLine("存入账户成功,当前您的余额为:{0}",yuanmoney );
Console.WriteLine();
}
}
/// <summary>
/// 定义取钱
/// </summary>
public static void makeMoney()
{
Console.Write("请输入要取的金额:");
int money = int.Parse(Console.ReadLine());
if (money >yuanmoney )
{
Console.WriteLine("余额不足,请充值!");
}
else if (yuanmoney -money <=1)
{
Console.WriteLine("请取至少100的现金!");
}
else
{
Console.WriteLine("取出金额" + money + "元");
yuanmoney = yuanmoney - money;
Console.WriteLine("取款成功!您卡上的余额为:{0}", yuanmoney );
Console.WriteLine();
}
}
/// <summary>
/// 定义查询余额
/// </summary>
public static void findMoney()
{
Console.WriteLine("您当前卡内余额为{0}",yuanmoney +"元");
Console.WriteLine();
}
/// <summary>
/// 修改密码
/// </summary>
public static bool updatePassword()
{
bool falg = true;
//接收新密码
Console.WriteLine("请输入新密码(六位整数)");
int pad1 = int.Parse(Console.ReadLine ());
Console.WriteLine("请再次输入新密码:");
int pad2 = int.Parse(Console.ReadLine ());
//判断密码更改成功
if ( password!=pad1)
{
Console.WriteLine("新密码不能与原密码一致!");
}
if (pad1 == pad2)
{
password = pad1;
int number = 3;
Console.WriteLine("银行卡已插入,请重新输入密码:");
int pad3 = int.Parse(Console.ReadLine());
while (number >0)
{
//判断
if (pad3 == password)
{
while (number ==1)
{
Console.WriteLine("长沙银行自主ATM机系统菜单");
Console.WriteLine("=========================");
Console.WriteLine("1.存钱\t\t2.取钱");
Console.WriteLine("3.查询余额\t4.修改密码");
Console.WriteLine("0.退卡");
Console.WriteLine();
Console.WriteLine("=========================");
Console.Write("请选择你要执行的操作:");
break;
}
}
else {
number--;
if (number > 0)
{
Console.WriteLine("密码输入错误,您还有{0}次输入", number);
}
else if (number <= 0)
{
Console.WriteLine("您三次密码输入错误,卡已冻结,请前往柜台寻求帮助!23");
}
}
}
}
else
{
Console.WriteLine("新密码两次输入不一致,更改失败!");
}
return falg;
}
static void Main(string[] args)
{
//密码登录
//定义登陆次数
int number = 3;
bool flag = true;
while (number > 0)
{
Console.Write("请输入密码(六位整数):");
int pad = int.Parse(Console.ReadLine ());
//判断
if (pad == password)
{
Console.WriteLine("登陆成功!");
Console.WriteLine();
Console.WriteLine("欢迎使用长沙银行自助服务ATM机!");
Console.WriteLine();Console.WriteLine("长沙银行自主ATM机系统菜单");
Console.WriteLine("=========================");
Console.WriteLine("1.存钱\t\t2.取钱");
Console.WriteLine("3.查询余额\t4.修改密码");
Console.WriteLine("0.退卡");
Console.WriteLine();
Console.WriteLine("=========================");
while (true )
{
Console.Write("请选择你要执行的操作:");
string num = Console.ReadLine();
switch (num)
{
case "1":
cun();
break;
case "2":
makeMoney();
break;
case "3":
findMoney();
break;
case "4":
updatePassword();
break;
case "0":
break ;
default:
Console.WriteLine("选择错误!");
break;
}
Console.WriteLine("欢迎您下次继续使用!");
}
}
else
{
number--;
if (number > 0)
{
Console.WriteLine("密码输入错误,您还有{0}次输入",number );
}
if (number == 0)
{
Console.WriteLine("您三次密码输入错误,卡已冻结,请前往柜台寻求帮助!23");
}
}
if (flag == false) { break; }
}
主要是定义修改密码的方法里面的判断出了错,跳不出来while语句
我在你的代码上修改了,看看是你要的效果吗:
/// <summary>
/// 修改密码
/// </summary>
public static bool updatePassword()
{
bool falg = true;
int result = 0;
first: Console.Write("请输入新密码(六位整数):");
string sInput = Console.ReadLine();//接收新密码
if (sInput.Length != 6 || sInput.Contains("-") || !int.TryParse(sInput, out result))
{
goto first;
}
int pad1 = int.Parse(sInput);
if (password == pad1)
{
Console.WriteLine("新密码不能与原密码一致!");
goto first;
}
second: Console.Write("请再次输入新密码:");
string sPwd2 = Console.ReadLine();
if (sPwd2 != sInput)
{
Console.WriteLine("两次输入新密码不一致!");
goto second;
}
int pad2 = int.Parse(sPwd2);
password = pad1;
int number = 3;//最多三次机会
while (number > 0)
{
Console.Write("密码修改成功,请输入密码重新登录:");
int pad3 = int.Parse(Console.ReadLine());
if (pad3 == password)
{
Console.WriteLine("登陆成功!");
Console.WriteLine();
Console.WriteLine("欢迎使用长沙银行自助服务ATM机!");
Console.WriteLine(); Console.WriteLine("长沙银行自主ATM机系统菜单");
Console.WriteLine("=========================");
Console.WriteLine("1.存钱\t\t2.取钱");
Console.WriteLine("3.查询余额\t4.修改密码");
Console.WriteLine("0.退卡");
Console.WriteLine();
Console.WriteLine("=========================");
return false;
}
else
{
number--;
if (number > 0)
{
Console.WriteLine("密码输入错误,您还可以输入{0}次", number);
}
else if (number <= 0)
{
Console.WriteLine("您三次密码输入错误,卡已冻结,请前往柜台寻求帮助!");
return false;
}
}
}
return falg;
}
switch里边的break只能跳出switch,想要跳出while循环需要设置个flag,在switch之外添加while的跳出判断if(flag) break;
或者使用goto
定义一个全局变量通过判断变量来跳出 或者 手动抛出异常就能跳出了
goto 因为太灵活,被各种规范禁用。