【问题标题】:C# , how to make a Key word to fulfill specific commandC#,如何制作一个关键字来完成特定的命令
【发布时间】:2021-05-30 10:18:07
【问题描述】:

我需要正确的command 帮助(请阅读下面code 中的问题,以便您准确理解我的问题, method: static void Main(string[] args))

namespace Bank
{
    class Bank
    {
        class BankAccount
        {
            private double balance = 0;
            public void Deposit(double n)
            {
                balance += n;
                Console.WriteLine($"You currently have {balance}");
            }
            public void Withdraw(double n)
            {


                  
                  

  if (balance >= n)
                { balance -= n;
                    Console.WriteLine($"After Withdrawal you have remained {balance}" + " in your Account");
                }
                else
                { Console.WriteLine("You need more money to Withdraw" + $"You currently have { balance}"); }
                
  

      }
        public double GetBalance()

        {
            Console.WriteLine($"Your Balance is : {balance}");
            return balance;
        }

    }
        static void Main(string[] args)
        {
        
        BankAccount Acc1 = new BankAccount();
        /* I want to type a specific key words that will allow me to proceed with certain command. 
         For example if I type "Deposit" it will proceed with the Deposit command below. And the same for Withdraw. But not both of them one by one (unless I type them both on purpose). */

        if (/* need correct command*/)
        { Acc1.Deposit(Convert.ToDouble(Console.ReadLine())); }

        else if (/* need correct command*/)
        { Acc1.Withdraw(Convert.ToDouble(Console.ReadLine())); }

        
    }
   
    }
}

【问题讨论】:

    标签: c# if-statement command key


    【解决方案1】:

    尝试将“存款”或“提款”放在一个字符串中,然后在 if 中比较该字符串。

    string type = 'Withdraw' if(type == 'withdraw) {...}
    

    【讨论】:

      猜你喜欢
      • 2018-02-08
      • 2015-07-05
      • 2016-01-07
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      相关资源
      最近更新 更多