我意识到我可以将我的数据放在构造函数调用的方法中,并且它可以工作。 但是,我只是搞乱了一些代码,我正在尝试使用其他方法进行计算。这是书中的一个问题,因为我没有一个教师会在几周内发现错误,我想我会问这里。
有什么输入?拜托,谢谢
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);
}
}
}
}
答案 0 :(得分:1)
IF 我理解你的问题,这应该适合你:
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);
}
}
答案 1 :(得分: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
,CostGas
和NumberGallons
)的任何字段时,相应的派生值(MilesPerGallon
并且CostPerMile
)将自动更正,无需任何其他方法调用,因为当有人访问这些字段时,它们是即时计算的。