0

我正在寻找将什么放入 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;
        }
    }

}
}
4

4 回答 4

2
var total = 0m;
total = purse.CountMoney(total);

但是,这段代码看起来有点奇怪,你应该传入一个双精度数,只是为了计算它并传回,而不考虑初始值。

就个人而言,我会丢失输入参数CountMoney()

 public double CountMoney()
        {
           //Existing Code
        }

并按照您的操作返回双精度,然后您可以直接调用它;

var total = purse.CountMoney();
于 2012-08-08T05:07:10.813 回答
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();
}
于 2012-08-08T05:11:49.357 回答
0

total请告诉,您的方法中参数的目的是什么CountMoney?是要检索总的钱数吗?然后你需要指定out关键字,可能不需要返回值,如下:

# 1(没有关键字)

    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();
    }

您也可以使用返回值以不同的方式进行操作(在这种情况下更自然)。

# 2(有返回值)

具有返回值的方法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();
    }
于 2012-08-08T05:12:58.913 回答
0

我认为您正在尝试添加(如果我错了,请纠正我)所有类型的更改以将您的钱全部放在钱包中,这在这种情况下不起作用,因为变量不会处理初始值并保存结果. 您应该使用 ref 关键字(请参阅以下内容:http: //msdn.microsoft.com/en-us/library/14akc2c7%28v=vs.71%29.aspx)或者最好的方法是使用对象(始终认为面向对象)

于 2012-08-08T05:15:13.927 回答