我正在上一个在线C#课程,它要求我填写一些空白,并且我发现了所有期望的内容: 在我的控制台应用程序中,我加了不同数量的钱,现在我试图显示它们。
我的最终结果应如下所示:
我的代码:
namespace CashRegister
{
class Program
{
static void Main(string[] args)
{
// declare a new CashRegister object
CashRegister myRegister = new CashRegister ();
// add several dollar amounts
myRegister.add (20.00);
myRegister.add (15.50);
myRegister.add (3.75);
// display the current cash balance
Console.WriteLine("The register has: $" + myRegister.????());
Console.ReadKey();
}
}
// CashRegister object definition
class CashRegister
{
// declare a property (class variable)
double cash = 0.0;
// define an add() method that takes one double "amount" parameter
public void add(double amount)
{
Console.WriteLine("Adding $" + amount);
cash += amount;
}
// define a report() method that returns one double value
public double report()
{
return cash;
}
}
}
我要在问号中插入什么以使最终结果与图片相同?
答案 0 :(得分:2)
您已经拥有的report()
方法返回值cash
。您所需要做的就是调用它:
Console.WriteLine("The register has: $" + myRegister.report());
// Here ---------------------------------------------^