Мне нужно сделать класс поездки, который принимает вход пользователя назначения, пройденного расстояния, стоимость газа и количество используемых галлонов и выводит мили за галлон и стоимость за милю.Метод класса поездки не работает должным образом
Почему-то мои mpg и cpm всегда выводят 0, что я делаю неправильно?
классПрограмма:
class Program
{
static void Main(string[] args)
{
Trip t1 = new Trip("Columbus, OH", 150, 2.95, 5);
Trip t2 = new Trip("Edmonton, AB", 3300, 2.75, 50);
Trip t3 = new Trip("Calgary, AB", 3100, 2.50, 45);
Console.WriteLine(t1);
Console.WriteLine(t2);
Console.WriteLine(t3);
}
}
Класс расцепления
class Trip
{
//class variables
string destination;
int distance;
double costOfGas;
int gallons;
int mpg;
double cpm;
//constructors
public Trip()
{
destination = "n.a.";
distance = 1;
costOfGas = 1;
gallons = 1;
}
public Trip(string destinationValue, int distanceValue,
double costOfGasValue, int gallonsValue)
{
Destination = destinationValue;
Distance = distanceValue;
CostOfGas = costOfGasValue;
Gallons = gallonsValue;
}
//properties
public string Destination
{
get { return destination; }
set { destination = value; }
}
public int Distance
{
get { return distance; }
set { distance = value; }
}
public double CostOfGas
{
get { return costOfGas; }
set { costOfGas = value; }
}
public int Gallons
{
get { return gallons; }
set { gallons = value; }
}
public int MPG
{
get { return mpg; }
set { mpg = MilesPerGallon(value); }
}
public double CPM
{
get { return cpm; }
set { cpm = CostPerMile(value); }
}
//user-defined methods
public int MilesPerGallon(int value)
{
return distance/gallons;
}
public double CostPerMile(double value)
{
return costOfGas * mpg;
}
override
public string ToString()
{
return "Trip["
+ destination + ", "
+ distance + ", "
+ costOfGas + ", "
+ gallons + "]"
+ "\n Miles Per Gallon = " + mpg
+ ", Cost Per Mile = " + cpm;
}
}
Выход:
Trip [Колумбус, Огайо, 150, 2,95, 5] миль на галлон = 0, цена за Mile = 0
и т.д.
Для вас нет никакого смысла разрешать настройку свойств 'MPG' и' CPM'. – juharr
, потому что вы никогда не называете CPM или CMP в своем коде, чтобы mpg и cmp filds никогда не менялись, они всегда 0 – Arash
Ваш код не имеет смысла. Вы пытались отладить его? – Biscuits