public decimal CurrentBalance = 1000.00m;
public decimal WithdrawCurrentAmount { get; set; }
public decimal MakeWithdraw()
{
Console.WriteLine("How much would you like to withdraw from your Current account?", WithdrawCurrentAmount);
if (WithdrawCurrentAmount < 0)
{
throw new Exception("You cannot withdraw a negative amount" );
}
WithdrawCurrentAmount = Convert.ToDecimal(Console.ReadLine());
CurrentBalance = CurrentBalance - WithdrawCurrentAmount;
Console.WriteLine("\nAvailable Current Balance is now: {0}", CurrentBalance);
return CurrentBalance;
您必须将Convert
行移至if语句之前,否则用户输入的值不会设置到您的测试变量中
您应该在检查之前分配值:
WithdrawCurrentAmount = Convert.ToDecimal(Console.ReadLine()); // this line now is before checking
if (WithdrawCurrentAmount < 0)
{
throw new Exception("You cannot withdraw a negative amount" );
}
CurrentBalance = CurrentBalance - WithdrawCurrentAmount;
Console.WriteLine("\nAvailable Current Balance is now: {0}", CurrentBalance);
return CurrentBalance;