Ai在这里是新手,也是编程新手。对不起,如果这个问题真的很幼稚,但我在一个简单的c#程序中使用返回类型时遇到了一些麻烦。 这是我的代码文件,在AccountTest类的d == 2,我希望提取过程重新开始,但这次没有要求用户输入帐户余额。朋友建议使用while循环,但我不知道如何在这里使用while循环。提前致谢。 :)
using System;
public class Account
{
public static void Main(string[] args)
{
Console.Write("Enter your account balance: ");
int AccountBalance = Convert.ToInt32(Console.ReadLine());
Account.Debit(AccountBalance);
}
public static void Debit(int AccountBalance)
{
Console.Write("\n\nEnter the amount you want to withdraw in Rs: ");
int WithdrawalAmount = Convert.ToInt32(Console.ReadLine());
AccountTest.DebitTest(AccountBalance, WithdrawalAmount);
}
}
我的帐户类
public class AccountTest
{
public static int DebitTest(int AccountBalance, int WithdrawalAmount)
{
if (WithdrawalAmount > AccountBalance)
{
Console.WriteLine("\n\nDebit amount exceeded account balance.");
Console.ReadLine();
return 0;
}
else if (WithdrawalAmount <= 0)
{
Console.WriteLine("\n\nError. Incorrect amount.");
Console.ReadLine();
return 0;
}
else
{
int newBalance = AccountBalance - WithdrawalAmount;
Console.WriteLine("\n\nWithdrawal was successful. Thankyou for using our services.\n\nPress 1 to exit, 2 to withdraw again, 3 to check your account balance.\n");
int InputNumber = Convert.ToInt32(Console.ReadLine());
if (InputNumber == 1)
{
Console.ReadLine();
return 0;
}
else if (InputNumber == 2)
{
return WithdrawalAmount;
}
else if (InputNumber == 3)
{
Console.WriteLine("\n\nYour remaining account balance is: {0}", newBalance);
Console.ReadLine();
return 0;
}
}
return 0;
}
}
答案 0 :(得分:3)
真的应该重构代码。处理像thing
这样的帐户更有意义,因为它应该是它自己的对象,你应该告诉它该怎么做:
public class Account
{
public int Balance { get; set; }
public Account(int startBalance)
{
Balance = startBalance;
}
public void Debit(int amount) { Balance -= amount; }
public void Credit(int amount) { Balance += amount; }
}
现在,您可以询问用户他们想要对自己的帐户执行的操作,并且您还可以添加多个帐户。所以该程序可能如下所示:
int startingAmount = int.Parse(Console.ReadLine());
var account = new Account(startingAmount);
Console.WriteLine("1 to credit, 2 to debit, 0 to exit");
var input = int.Parse(Console.ReadLine());
while (input != 0)
{
//manipulate account
}
我首先阅读静态vs实例对象
答案 1 :(得分:1)
首先,欢迎来到编码社区,没有幼稚的问题随时可以随意询问,有些人会用“谷歌有答案”回答你,但不用担心很多其他人会帮助你,我看到你已经选择了一个答案,但生病了,为你和其他新程序员添加了我的观点。
一个。如果您的新编码永远不会从代码开始,它只会使事情变得复杂,而是从流程图开始,说明您从程序和每个组件(类,函数等)中获得的内容后,它更容易要将其转换为代码,您可以尝试使用this site 它似乎非常用户友好,并将以正确的格式绘制流程图。
湾就像我这里所说的人从来没有像a,b,c那样使用变量,因为第二天你会试着继续你离开的地方,你会忘记你的意思。
℃。试着想办法使用代码来防止重复自己。
d。使用硬编码值是不好的做法(硬编码意味着:
return "this value to return is hard coded and will never change";
)
当我回答你的问题时,我已经看到了@Jonesy的答案,这是正确的,喜欢什么 我想建议如此生病,留下你的答案。
希望这有助于某人:)
答案 2 :(得分:0)
循环可以在借记方法中实现:
public static void Debit(int AccountBalance)
{
int result = 0;
do
{
Console.Write("\n\nEnter the amount you want to withdraw in Rs: ");
var WithdrawalAmount = Convert.ToInt32(Console.ReadLine());
result = AccountTest.DebitTest(AccountBalance, WithdrawalAmount);
} while (result != 0);
}
答案 3 :(得分:0)
你应该阅读while循环。基本上你想要的是返回一个数字的方法,该数字决定了程序下一步应该做什么,或者应该什么时候结束。将循环视为您的引擎状态&#34;只要没有按下[选择的键],就继续做某事&#34;。
一个小例子,[selected key]为1,将是:
int choice = 0;
int balance = 100;
while (choice != 1) {
Console.WriteLine("Enter withdraw amount");
string userInput = Console.ReadLine();
// This will crash the program if the user enters text.
// Used for demonstration only. int.TryParse is preferred.
int value = int.Parse(userInput);
choice = AccountTest.DebitTest(balance, value);
}
class AccountTest {
public static int DebitTest(int AccountBalance, int WithdrawalAmount)
{
// do the test if the debit is OK
//..........
// At the end, you can do this. This till return the value entered and
// choice will be given a new value. If it was 1, the program will end.
// NOTE: It's not safe to use convert as
// it will crash the program if the user enters text.
return Convert.ToInt32(Console.ReadLine());
}
}
请注意,这不是一个功能性的ATM程序,因为余额永远不会更新。我留下那个让你解决,因为我猜这是编程中的一个类=)
答案 4 :(得分:0)
嗯,这是我的程序版本。我稍微改变了行为(比如当值无效时再次询问)。它并不完美;例如,消息和RequestDebit
方法应该在Account
类之外处理,可能在AccountHandler
类中处理,但对于这个简单的练习,所有这些都可能有点过分。无论如何,我希望你发现它很有用:
public class Account
{
public int Balance { get; set; }
public Account(int startingBalance)
{
this.Balance = startingBalance;
}
private bool Debit(int amount)
{
if (amount <= 0)
{
Console.WriteLine("\n\nError. Incorrect amount.");
return false;
}
if (amount > this.Balance)
{
Console.WriteLine("\n\nDebit amount exceeded account balance.");
return false;
}
this.Balance -= amount;
Console.WriteLine("\n\nWithdrawal was successful. Thankyou for using our services.");
return true;
}
public void RequestDebit()
{
bool success;
do
{
Console.Write("\n\nEnter the amount you want to withdraw in Rs: ");
int withdrawalAmount = Convert.ToInt32(Console.ReadLine());
success = this.Debit(withdrawalAmount);
} while (!success);
}
}
class Program
{
static void Main(string[] args)
{
int accountBalance;
do
{
Console.Write("Enter your account balance: ");
accountBalance = Convert.ToInt32(Console.ReadLine());
} while (accountBalance <= 0);
var myAccount = new Account(accountBalance);
myAccount.RequestDebit();
int inputNumber;
do
{
Console.WriteLine("\n\nPress 1 to exit, 2 to withdraw again, 3 to check your account balance.\n");
inputNumber = Convert.ToInt32(Console.ReadLine());
switch (inputNumber)
{
case 2: myAccount.RequestDebit();
break;
case 3:
Console.WriteLine("\n\nYour remaining account balance is: {0}", myAccount.Balance);
break;
}
} while (inputNumber != 1);
}
}