I am struggling to create a program that records the balance of a user. I have constructed four methods that are used to manage the user's method. The methods include Main(), GetAmount(), Withdraw(), and Deposit. Each method performs a specific operation on the data. However, the GetAmount() method continuously loops without returning the amount inputted. I have attempted to use break statements to break out of the loop in the GetAmount() method however it continuously loops the entire method. I would like to check if the input is double. The input should be passed to the method that performs the operation chosen by the user.
public static void Main() //THIS IS A COPY OF MY CODE
{
char transaction;
Console.WriteLine("Please enter a character 'W' to withdrawl, 'D' to deposit, 'P' to print balance, 'Q' to quit program)");
while (!char.TryParse(Console.ReadLine(), out transaction) )
{
Console.WriteLine("Please enter valid character: ");
}
double balance = 0;
double amount = GetAmount();
do
{
switch (transaction)
{
case 'w':
case 'W':
{
Withdrawal(amount, ref balance);
return;
}
case 'd':
case 'D':
{
Deposit(amount, ref balance);
return;
}
case 'p':
case 'P':
{
Print(balance);
return;
}
}
} while (char.ToUpper(transaction) != 'Q');
Console.WriteLine("You have typed Q to quit the program!");
Console.ReadLine();
}
public static double GetAmount()
{
double amount;
Console.WriteLine("Please enter amount to withdraw or deposit");
do
{
if (!double.TryParse(Console.ReadLine(), out amount))
{
Console.WriteLine("Please enter a valid amount.");
break;
}
if (amount < 0)
{
Console.WriteLine("Please enter a value that is > 0.");
break;
}
} while (amount > 0);
return GetAmount();
}
public static void Withdrawal(double amount, ref double balance)
{
balance -= amount - 1.5;
return;
}
public static void Deposit(double amount, ref double balance)
{
if (amount > 2000)
{
balance += amount * 1.01;
}
else
{
balance += amount;
}
return;
}
public static void Print(double balance)
{
Console.WriteLine("Your total balance is {0:C}.", balance);
return;
}
At the end of the GetAmount()
method, say:
return amount;
instead of:
return GetAmount();
The latter is incorrect because it is a recursive method call: https://en.wikipedia.org/wiki/Recursion
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.