好吧,所以我有这个课程的测试人员,它一直在说以下消息:
“现在测试您的驱动方法:
***行驶700英里,行驶400英里,然后用完汽油的汽车现在不应具有1700.0英里的行驶里程。”
但是,当我删除里程或gasNeeded或gasInTank时,它会说正在运行的那个正在运行,但不能同时运行!
任何帮助表示赞赏!
public class Car
{
private double mpg;
private double mileage;
private double tankCapacity;
private double gasInTank;
/**
Constructs a car.
@param miles miles per gallon that a car gets
@param milesOnCar total mileage on car
@param gasTankHolds amount of gas the tank holds
@param gasTankHas gas in tank now
*/
public Car(double miles, double milesOnCar,
double gasTankHolds, double gasTankHas)
{
mpg = miles;
mileage = milesOnCar;
tankCapacity = gasTankHolds;
gasInTank = gasTankHas;
}
/**
Returns the current mileage on the car.
@return mileage on car
*/
public double getMileage()
{
return mileage;
}
/**
Returns the amount of gas in the gas tank.
@return amount of gas in tank
*/
public double getGasInTank()
{
return gasInTank;
}
/**
Returns the gas needed to drive miles (based on mpg)
@param numMiles the number of miles to be driven
@return gas needed to drive numMiles miles
*/
public double gasNeeded(double numMiles)
{
double gasNeeded = numMiles/mpg ;
if(numMiles == 0)
{
return 0 ;
}
else if(mpg == 0)
{
return 0 ;
}
else
return gasNeeded ;
}
/**
Returns true if the car has enough gas in the tank to
drive numMiles, otherwise returns false.
@param numMiles miles to be driven
@return true if gas in tank is enough to drive numMiles
miles; false otherwise.
*/
public boolean enoughGas(double numMiles)
{
double gasNeeded = numMiles/mpg ;
if(gasInTank == gasNeeded)
{
return true ;
}
else
{
return false;
}
}
/**
If tank is less than half full, fills tank and updates
gasInTank. Otherwise does nothing.
*/
public void getGas()
{ double gas2 = 1.0/2.0 * tankCapacity ;
if(gasInTank <= gas2)
{
gasInTank = tankCapacity ;
}
}
/**
Updates mileage and gasInTank to reflect numMiles
being driven.
@param numMiles number of miles driven
*/ /**
Updates mileage and gasInTank to reflect numMiles
being driven.
@param numMiles number of miles driven
*/
public void drive(double numMiles)
{
double gasNeeded = numMiles/mpg ;
if(numMiles != 0 )
{
mileage = mileage + numMiles ;
}
if(gasInTank> gasNeeded)
{
gasInTank = gasInTank - gasNeeded ;
}
if(gasInTank < gasNeeded){
gasInTank = 0 ;
}
}
}