首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在此场景中如何从用户获取输入

在此场景中如何从用户获取输入
EN

Stack Overflow用户
提问于 2021-02-18 22:55:48
回答 1查看 49关注 0票数 0

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

代码语言:javascript
运行
复制
        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());

我是否也需要在构造函数中做一些更改?这是我的构造函数

代码语言:javascript
运行
复制
    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");
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-19 18:07:00

根据@Johnny Mopp,您可以使用Console.ReadLine()从用户获取输入。

例如:

代码语言:javascript
运行
复制
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.");
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66262443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档