在此场景中如何从User获取输入



我是编程新手,RightNow我正在跟随一些在线教程(来自微软)。这是我的代码,到目前为止,我硬编码了像现金存入或现金提取这样的值,我的问题很简单,如何从用户那里获取输入。

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.");
}

最新更新