0

我意识到我可以将我的数据放入我的构造函数正在调用的方法中并且它会起作用。但是,我只是在弄乱一些代码,并且正在尝试使用其他方法进行计算。这是一本书中的一个问题,由于我有几个星期没有教练可以解决问题,所以我想我会在这里问。

有输入吗?谢谢,麻烦您了

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace ConsoleApplication305
{
class Program
{
    static void Main(string[] args)
    {
        Trip a = new Trip();
        Console.WriteLine(a);

    }

    class Trip
    {
        int distanceTraveled;
        double costGas;
        int numberGallon;
        int mpg;
        double cpm;

        public int DistanceTraveled
        {
            get
            {
                return distanceTraveled;
            }
        }
        public double CostGas
        {
            get
            {
                return costGas;
            }
        }
        public int NumberGallon
        {
            get
            {
                return numberGallon;
            }
        }

        public Trip()
        {
            distanceTraveled = 312;
            costGas = 3.46;
            numberGallon = 10;

        }

        public void milesPerGal()
        {
            mpg = distanceTraveled / numberGallon;
        }

        public void costPerMile()
        {
            cpm = costGas * mpg;
        }

        public override string ToString()
        {
            return String.Format("The distance traveled is...{0} \nThe cost per gallon of gasoline is... {1} \nThe amount of gallons were... {2} \nThe miles per gallon attained were... {3} \nThe cost per MPG were... {4}", distanceTraveled, costGas, numberGallon, mpg, cpm);
        }
    }
}

}

4

2 回答 2

1

如果我正确理解您的问题,这应该对您有用:

class Trip
{
    int distanceTraveled;
    double costGas;
    int numberGallon;
    int mpg;
    double cpm;

    public int DistanceTraveled
    {
        get
        {
            return distanceTraveled;
        }
    }
    public double CostGas
    {
        get
        {
            return costGas;
        }
    }
    public int NumberGallon
    {
        get
        {
            return numberGallon;
        }
    }

    public Trip()
    {
        distanceTraveled = 312;
        costGas = 3.46;
        numberGallon = 10;
        mpg = milesPerGal();
        cpm = costPerMile();
    }

    public int milesPerGal()
    {
        return distanceTraveled / numberGallon;
    }

    public double costPerMile()
    {
        return costGas * mpg;
    }

    public override string ToString()
    {
        return String.Format("The distance traveled is...{0} \nThe cost per gallon of gasoline is... {1} \nThe amount of gallons were... {2} \nThe miles per gallon attained were... {3} \nThe cost per MPG were... {4}", distanceTraveled, costGas, numberGallon, mpg, cpm);
    }
}
于 2013-08-16T00:41:33.007 回答
1

老实说,这里一切正常。由于您从未调用过方法milesPerGal 或costPerMile,因此私有变量mpg 和cpm 的值仍为0,这也将被输出。

您可能想要的是这个构造函数:

public Trip()
{
    distanceTraveled = 312;
    costGas = 3.46;
    numberGallon = 10;
    milesPerGal();
    costPerMile();
}

为了使此代码按预期工作,我建议您使用正确的 getter 和 setter,如下所示:

class Trip
{
    public int DistanceTraveled { get; set; }
    public double CostGas { get; set; }
    public int NumberGallon { get; set; }
    public int MilesPerGallon { get { return (NumberGallon != 0) ? DistanceTraveled / NumberGallon : 0; } }
    public double CostPerMile { get { return CostGas * MilesPerGallon; } }

    public Trip()
    {
        DistanceTraveled = 312;
        CostGas = 3.46;
        NumberGallon = 10;
    }

    public override string ToString()
    {
        return String.Format("The distance traveled is...{0} \nThe cost per gallon of gasoline is... {1} \nThe amount of gallons were... {2} \nThe miles per gallon attained were... {3} \nThe cost per MPG were... {4}", DistanceTraveled, CostGas, NumberGallon, MilesPerGallon, CostPerMile);
    }
}

请注意,使用这种方法,每当您更新任何具有 setter (和) 的字段时DistanceTraveled,相应的派生值 (和) 将自动正确,无需任何额外的方法调用,因为它们是动态计算的,当有人访问这些字段。CostGasNumberGallonsMilesPerGallonCostPerMile

于 2013-08-16T00:43:14.963 回答