我正在寻找放入purse.CountMoney()的参数的内容;如果有的话......我有一个心理障碍,因为返回一个值的方法与非返回方法相比有效...我试过总...双总....我试着踩到程序中,看看是否调试工具可以提供帮助,没有任何东西突然出现在我身上。
任何指导都很精彩。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lab24
{
class Program
{
static void Main()
{
CoinPurse purse = new CoinPurse();
purse.AddPenny(3);
purse.AddNickel(4);
purse.AddDime(2);
purse.AddQuarter(1);
purse.CountMoney();
purse.TakeNickels(4);
purse.CountMoney();
Console.ReadLine();
}
public class CoinPurse
{
private int numPennies;
private int numNickels;
private int numDimes;
private int numQuarters;
private double total;
public CoinPurse()
{
numPennies = 0;
numNickels = 0;
numDimes = 0;
numQuarters = 0;
total = 0;
}
public void AddPenny(int p)
{
numPennies += p;
}
public void AddNickel(int n)
{
numNickels += n;
}
public void AddDime(int d)
{
numDimes += d;
}
public void AddQuarter(int q)
{
numQuarters += q;
}
public void TakePenny(int p)
{
numPennies -= p;
}
public void TakeNickels(int n)
{
numNickels -= n;
}
public void TakeDime(int d)
{
numDimes -= d;
}
public void TakeQuarters(int q)
{
numQuarters -= q;
}
public double CountMoney(double total)
{
double q = numQuarters * .25;
double d = numDimes * .10;
double n = numNickels * .05;
double p = numPennies * .01;
total = q + d + n + p;
return total;
}
}
}
}
答案 0 :(得分:2)
var total = 0m;
total = purse.CountMoney(total);
然而,这段代码看起来有点奇怪,你需要传递一个double,只是为了计算它并传回去,而不考虑初始值。
就个人而言,我会将输入参数丢失为CountMoney()
public double CountMoney()
{
//Existing Code
}
并且只是在你做的时候返回双倍,你可以直接调用它;
var total = purse.CountMoney();
答案 1 :(得分:2)
将您的CountMoney
方法更改为:
public double CountMoney()
{
double q = numQuarters * .25;
double d = numDimes * .10;
double n = numNickels * .05;
double p = numPennies * .01;
double total = q + d + n + p;
return total;
}
然后,您可以使用Main
方法调用它:
static void Main()
{
CoinPurse purse = new CoinPurse();
purse.AddPenny(3);
purse.AddNickel(4);
purse.AddDime(2);
purse.AddQuarter(1);
purse.CountMoney();
purse.TakeNickels(4);
double total = purse.CountMoney();
Console.WriteLine(total);
Console.ReadLine();
}
答案 2 :(得分:0)
请告诉您方法total
中参数CountMoney
的目的是什么?是要检索总金额吗?然后,您需要指定out
关键字,可能不需要返回值,如下所示:
public void CountMoney(out double total)
{
double q = numQuarters * .25;
double d = numDimes * .10;
double n = numNickels * .05;
double p = numPennies * .01;
total = q + d + n + p;
}
然后方法Main
将如下所示:
static void Main()
{
CoinPurse purse = new CoinPurse();
purse.AddPenny(3);
purse.AddNickel(4);
purse.AddDime(2);
purse.AddQuarter(1);
purse.CountMoney();
purse.TakeNickels(4);
double total;
purse.CountMoney(out total);
Console.WriteLine(total);
Console.ReadLine();
}
此外,您可以使用返回值以不同的方式(对于这种情况更自然地)执行此操作。
具有返回值的方法CountMoney
将如下所示:
public double CountMoney()
{
double q = numQuarters * .25;
double d = numDimes * .10;
double n = numNickels * .05;
double p = numPennies * .01;
return q + d + n + p;
}
和方法Main
将如下所示:
static void Main()
{
CoinPurse purse = new CoinPurse();
purse.AddPenny(3);
purse.AddNickel(4);
purse.AddDime(2);
purse.AddQuarter(1);
purse.CountMoney();
purse.TakeNickels(4);
Console.WriteLine(purse.CountMoney());
Console.ReadLine();
}
答案 3 :(得分:0)
我认为你试图添加(纠正我,如果我错了)所有类型的更改,以便在钱包中获得总金额,这在这种情况下不起作用,因为变量不会处理初始值和保持结果。你应该使用ref关键字(参见下面的内容:http://msdn.microsoft.com/en-us/library/14akc2c7%28v=vs.71%29.aspx),或者最好的方法是利用对象(总是认为面向对象)