在这种情况下如何从用户那里获取输入
How to take input from User in this scenario
我是编程新手,现在我正在学习一些在线教程(来自 Microsoft)。这是我的代码,到目前为止我硬编码了现金存款或现金提取等值,我的问题很简单如何从用户那里获取输入。
var savings = new SavingAccount("Saving Account", 10000);
savings.MakeDeposite(1000, DateTime.Now, "Saving from Laptop");
savings.MakeDeposite(525, DateTime.Now, "Saving from Hard-drive");
savings.MakeWithdrawal(2500, DateTime.Now, "Needed for buying goods");
savings.PerformMonthEndTransactions();
Console.WriteLine(savings.GetAccountHistory());
var currents = new CurrentAccount("Current Account", 0, 1000);
currents.MakeDeposite(150000, DateTime.Now, "Salary Transfered");
currents.MakeWithdrawal(25000, DateTime.Now, "Pay bills");
currents.MakeWithdrawal(50000, DateTime.Now, "Rent");
currents.PerformMonthEndTransactions();
Console.WriteLine(currents.GetAccountHistory());
我是否也需要对构造函数进行一些更改?这是我的构造函数
public BankAccount(string name, decimal initialBalance) : this(name, initialBalance, 0) { }
public BankAccount(string name, decimal initialBalance, decimal minimumBalance)
{
//this.Owner = name;
//this.Balance = initialBalance;
this.Number = accountNumberSeed.ToString();
accountNumberSeed++;
this.Owner = name;
this.minimumBalance = minimumBalance;
if(initialBalance > 0)
MakeDeposite(initialBalance, DateTime.Now, "Initial Balance");
}
根据@Johnny Mopp,您可以使用Console.ReadLine()
获取用户的输入。
例如:
static void Main(string[] args)
{
Console.Write("Specify your amount: ");
if (decimal.TryParse(Console.ReadLine(), out decimal amount))
Console.WriteLine("amount entered: " + amount);
else
Console.WriteLine("Conversion of user input to decimal failed.");
}
我是编程新手,现在我正在学习一些在线教程(来自 Microsoft)。这是我的代码,到目前为止我硬编码了现金存款或现金提取等值,我的问题很简单如何从用户那里获取输入。
var savings = new SavingAccount("Saving Account", 10000);
savings.MakeDeposite(1000, DateTime.Now, "Saving from Laptop");
savings.MakeDeposite(525, DateTime.Now, "Saving from Hard-drive");
savings.MakeWithdrawal(2500, DateTime.Now, "Needed for buying goods");
savings.PerformMonthEndTransactions();
Console.WriteLine(savings.GetAccountHistory());
var currents = new CurrentAccount("Current Account", 0, 1000);
currents.MakeDeposite(150000, DateTime.Now, "Salary Transfered");
currents.MakeWithdrawal(25000, DateTime.Now, "Pay bills");
currents.MakeWithdrawal(50000, DateTime.Now, "Rent");
currents.PerformMonthEndTransactions();
Console.WriteLine(currents.GetAccountHistory());
我是否也需要对构造函数进行一些更改?这是我的构造函数
public BankAccount(string name, decimal initialBalance) : this(name, initialBalance, 0) { }
public BankAccount(string name, decimal initialBalance, decimal minimumBalance)
{
//this.Owner = name;
//this.Balance = initialBalance;
this.Number = accountNumberSeed.ToString();
accountNumberSeed++;
this.Owner = name;
this.minimumBalance = minimumBalance;
if(initialBalance > 0)
MakeDeposite(initialBalance, DateTime.Now, "Initial Balance");
}
根据@Johnny Mopp,您可以使用Console.ReadLine()
获取用户的输入。
例如:
static void Main(string[] args)
{
Console.Write("Specify your amount: ");
if (decimal.TryParse(Console.ReadLine(), out decimal amount))
Console.WriteLine("amount entered: " + amount);
else
Console.WriteLine("Conversion of user input to decimal failed.");
}