面向初学者的 Java 汽车课程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28596814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java car class for beginner
提问by flame123536
I must Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon or liters/km—pick one) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. Supply a method drive that simulates driving the car for a certain distance, reducing the amount of gasoline in the fuel tank. Also supply methods getGasInTank, returning the current amount of gasoline in the fuel tank, and addGas, to add gasoline to the fuel tank.
我必须实现一个具有以下属性的类 Car。一辆汽车有一定的燃油效率(以英里/加仑或升/公里为单位——选择一个)和油箱中一定量的燃料。效率在构造函数中指定,初始油位为0。提供一个方法drive,模拟汽车行驶一定距离,减少油箱中的汽油量。还提供方法 getGasInTank,返回当前油箱中的汽油量,和 addGas,将汽油添加到油箱。
I have created a class for the car and a test program to plug some values in and when i run the program all i get returned is the addGas value that i put in. the computation for the miles per gallon will not run and i do not understand why. as you can probably tell i am VERY new at java and any help on the issue is much appreciated.
我已经为汽车创建了一个类和一个测试程序来插入一些值,当我运行该程序时,我返回的只是我输入的 addGas 值。每加仑英里数的计算将不会运行,我也不会明白为什么。正如您可能会说我对 Java 非常陌生,非常感谢您对这个问题的任何帮助。
public class CarTest
{
public static void main(String[] args)
{
Car richCar = new Car(49);
richCar.addGas(15);
richCar.drive(150);
System.out.println(richCar.getGas());
}
}
/**
A car can drive and consume fuel
*/
public class Car
{
/**
Constructs a car with a given fuel efficiency
@param anEfficiency the fuel efficiency of the car
*/
public Car(double mpg)
{
milesPerGallon = mpg;
gas = 0;
drive = 0;
}
/** Adds gas to the tank.
@param amount the amount of fuel added
*/
public void addGas(double amount)
{
gas = gas + amount;
}
/**
Drives a certain amount, consuming gas
@param distance the distance driven
*/
public void drive(double distance)
{
drive = drive + distance;
}
/**
Gets the amount of gas left in the tank.
@return the amount of gas
*/
public double getGas()
{
double mpg;
double distance;
distance = drive;
mpg = gas * milesPerGallon / distance;
return gas;
}
private double drive;
private double gas;
private double milesPerGallon;
}
采纳答案by shirrine
Combining some ideas...
结合一些想法...
/**
Drives a certain amount, consuming gas
@param distance the distance driven
*/
public void drive(double distance)
{
drive = drive + distance;
gas = gas - (distance / milesPerGallon);
}
/**
Gets the amount of gas left in the tank.
@return the amount of gas
*/
public double getGas()
{
return gas;
}
回答by Makoto
It's computingit just fine. Your method is only returningthe one value for gas, though.
它计算得很好。不过,您的方法仅返回gas 的一个值。
This could use some clean-up; I'd suggest getting rid of all of the cruft, and just returning the calculation.
这可能需要一些清理工作;我建议去掉所有的杂物,然后返回计算。
public double calculateGas() {
return (gas * milesPerGallon) / drive;
}
回答by NESPowerGlove
Your test program only calls three methods:
您的测试程序仅调用三个方法:
richCar.addGas(15);
richCar.drive(150);
System.out.println(richCar.getGas());
Let's take a look at what each one does.
让我们来看看每个人都做了什么。
addGas(15)
modifies your gas instance variable.
addGas(15)
修改您的 gas 实例变量。
drive(150)
only executes the line drive = drive + distance;
drive(150)
只执行该行 drive = drive + distance;
getGas()
seems to do a couple things, more than expected (one would expect it just returns gas
but it also calculates and stores latest mpg value). But if you check the code of getGas()
gas is never modified, only returned.
getGas()
似乎做了几件事,比预期的要多(人们会期望它只是返回,gas
但它还会计算并存储最新的 mpg 值)。但是如果你检查getGas()
gas的代码是永远不会被修改的,只会返回。
So you called three methods, and within those three executions gas is only modified once, when you set it to the value of 15 with richCar.addGas(15)
.
所以你调用了三个方法,在这三个执行中,gas 只被修改一次,当你用richCar.addGas(15)
.
It would seem most simple if the drive(int)
method modifys gas
based on some set mpg value, instead of only keeping track of how many miles were driven, and then you could have drive()
simply just return gas
.
如果该drive(int)
方法gas
根据某个设置的 mpg 值进行修改,而不是仅跟踪行驶了多少英里,那么看起来最简单,然后您就可以drive()
简单地返回gas
。
That means you would also get rid of modifying mpg
in getGas()
, which is good, because that value is needed no matter what to calculate how much gas is used. You could perhaps introduce a actualMpg
value if you had some more logic that changed how much gas is used but you don't have that yet.
这意味着您也将摆脱修改mpg
in getGas()
,这很好,因为无论计算使用多少气体都需要该值。actualMpg
如果您有更多的逻辑来改变使用的 gas 数量,您也许可以引入一个值,但您还没有。
回答by DotNet NF
From what it looks like your problem lies here
从看起来你的问题就在这里
public double getGas()
{
double mpg;
double distance;
distance = drive;
mpg = gas * milesPerGallon / distance;
return gas;
}
you are doing a calculation to mpg however you are returning the gas property at the end.
您正在对 mpg 进行计算,但是最后您将返回 gas 属性。
It doesn't actually seem like you are changing the value of gas anywhere except for when you first add it.
除了第一次添加它时,您实际上似乎并没有在任何地方更改 gas 的值。