我意识到我可以将我的数据放入我的构造函数正在调用的方法中并且它会起作用。但是,我只是在弄乱一些代码,并且正在尝试使用其他方法进行计算。这是一本书中的一个问题,由于我有几个星期没有教练可以解决问题,所以我想我会在这里问。
有输入吗?谢谢,麻烦您了
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);
}
}
}
}