问题:如果您存入 1000-->然后借入 40000-->然后提取 43000 并收到错误“余额不足”-->然后重试并存入 28000。这里的问题是余额为 -31000第二次你尝试而不是 12000 左右的东西。有人知道为什么吗?
我尝试将大写“语句”移至 else 语句中,但这只会让事情变得更糟。我还尝试将其移至帐户统计部分,但这也不起作用。我对此很陌生,而且我不太擅长解决问题,所以很抱歉,如果这是显而易见的事情:/
using System;
namespace atmtest
{
class Program
{
public static void Main()
{
int balance = 0, deposit = 0, withdraw = 0, choice, loan = 0, kredit = 0, debt = 0, capital = 0;
while (true)
{
Console.WriteLine("----------------------------");
Console.WriteLine("Welcome");
Console.WriteLine("----------------------------");
Console.WriteLine("1) Deposit");
Console.WriteLine("2) Withdraw");
Console.WriteLine("3) Account stats");
Console.WriteLine("4) Borrow money");
Console.WriteLine("--------------------------------");
Console.WriteLine("Enter one of the options ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.Clear();
try
{
Console.WriteLine("Deposit, enter amount : ");
deposit = int.Parse(Console.ReadLine());
if (deposit > 500000)
{
Console.WriteLine("Error, max amount 500000");
}
else if (deposit < 0)
{
Console.WriteLine("Error, the amount has to be positive");
}
else
{
capital = balance + deposit;
Console.WriteLine("You have deposited " + deposit + " and your balance is now: " + capital);
}
}
catch
{
Console.WriteLine("Error, enter numbers only.");
}
break;
case 2:
Console.Clear();
Console.WriteLine("Withdraw, enter amount: ");
withdraw = int.Parse(Console.ReadLine());
capital = kredit + balance - withdraw;
balance = balance - withdraw;
if ((withdraw > balance) && (withdraw > kredit))
{
Console.WriteLine("Error, insufficent balance.");
capital = deposit;
Console.WriteLine("Try again");
}
else
{
debt = balance;
Console.WriteLine("You have withdrawn " + withdraw);
Console.WriteLine("Your current balance is " + capital);
}
break;
case 3:
Console.Clear();
Console.WriteLine("Account stats: ");
Console.WriteLine("Balance: " + capital);
Console.WriteLine("Amount of money borrowed ( loan ): " + kredit);
Console.WriteLine("Debt: " + debt);
Console.WriteLine("You have used " + debt );
break;
case 4:
Console.Clear();
Console.WriteLine("how much do you want to borrow? Max amount is 50000.");
Console.WriteLine("Enter amount: ");
loan = int.Parse(Console.ReadLine());
kredit = loan + kredit;
if (kredit > 50000)
{
Console.WriteLine("You cannot borrow this amount, max amount is 50000");
kredit = kredit - loan;
}
else if (kredit <= 50000)
{
Console.WriteLine("You have been granted " + loan);
}
break;
}
}
}
}
}
最佳答案
尝试这个小改变来跟踪您的变量值,实际上您收到的消息是正确的。这些仅基于您的值(value)观分配。
请更改代码片段并尝试运行:
int balance = 0, deposit = 0, withdraw = 0, choice, loan = 0, kredit = 0, debt = 0, capital = 0;
StringBuilder audit = new StringBuilder();
while (true)
{
Console.WriteLine("----------------------------");
Console.WriteLine("Welcome");
Console.WriteLine("----------------------------");
Console.WriteLine("Audit");
audit.Append($" balance = {balance}, deposit = {deposit}, withdraw = {withdraw}, loan = {loan}, kredit = {kredit}, debt = {debt}, capital = {capital} \n");
Console.WriteLine(audit.ToString());
Console.WriteLine("----------------------------");
Console.WriteLine("1) Deposit");
Console.WriteLine("2) Withdraw");
Console.WriteLine("3) Account stats");
Console.WriteLine("4) Borrow money");
Console.WriteLine("--------------------------------");
Console.WriteLine("Enter one of the options ");
....
....
很明显,提款后余额没有减少,因此需要在逻辑上进行编程修正。
关于c# - 提款和余额出现问题,第二次尝试时计算无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67567194/