gpt4 book ai didi

java - 简单的 MPG 逻辑

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:44 25 4
gpt4 key购买 nike

我正在尝试制作一个 MPG 计算器,这里是测试用例和预期结果:

我的测试员:

    Car auto = new Car(15);
System.out.println("New car odometer reading: ");
auto.fillUp(150,8);
System.out.println("Miles per gallon" + auto.calculateMPG());
System.out.println("Miles per gallon" + auto.calculateMPG());
auto.resetMPG();
auto.fillUp(350, 10);
auto.fillUp(450, 20);
System.out.println("Miles per gallon" + auto.calculateMPG());
auto.resetMPG();
auto.fillUp(603, 25.5);
System.out.println("Miles per gallon" + auto.calculateMPG());

预期结果:

New car odometer reading: 15
Miles per gallon: 16.875
Miles per gallon: 16.875
Miles per gallon: 10.0
Miles per gallon: 6.0

我得到的:

New car odometer reading: 
Miles per gallon16.875
Miles per gallon16.875
Miles per gallon15.0
Miles per gallon23.647058823529413

我认为问题出在我的填充代码上,但我太笨了,无法弄清楚......

public class Car
{
private int startMiles;
private int endMiles;
private double gallonsUsed;
private int odometerReading;

public Car(int odometerReading) {
startMiles = odometerReading;
endMiles = odometerReading;
}

public void fillUp (int odometerReading, double gallons) {
endMiles = odometerReading;
gallonsUsed = gallonsUsed + gallons;
}

public double calculateMPG() {
double MPG = (endMiles-startMiles)/gallonsUsed;
return MPG;
}

public void resetMPG() {
gallonsUsed = 0;
startMiles = odometerReading;
endMiles = odometerReading;
}
}

求助!我想不通。

最佳答案

您的问题是您永远不会提高 Car 对象的 odometerReading 属性(它始终为零)。当您不初始化原始 int 时,it defaults to 0 .

一个快速的解决方法是不要将您的开始和结束里程设置为您的 odometerReading 属性,而是让您的 startMiles 等于您的 endMiles

通过此更改,您还可以删除 int odometerReading 属性,因为它将不再被使用:

public class Car
{
private int startMiles;
private int endMiles;
private double gallonsUsed;

public Car(int odometerReading) {
startMiles = odometerReading;
endMiles = odometerReading;
}

public void fillUp (int odometerReading, double gallons) {
endMiles = odometerReading;
gallonsUsed = gallonsUsed + gallons;
}

public double calculateMPG() {
double MPG = (endMiles-startMiles)/gallonsUsed;
return MPG;
}

public void resetMPG() {
gallonsUsed = 0;
startMiles = endMiles;
}
}

新输出:

New car odometer reading:
Miles per gallon16.875
Miles per gallon16.875
Miles per gallon10.0
Miles per gallon6.0


如果您希望输出符合您的预期,请对您的主类进行以下更改:

public static void main(String[] args) {
Car auto = new Car(15);
System.out.println("New car odometer reading: " + auto.getEndMiles());
auto.fillUp(150,8); //^^Call a getter method
System.out.println("Miles per gallon: " + auto.calculateMPG());
System.out.println("Miles per gallon: " + auto.calculateMPG());
auto.resetMPG(); //^ just add a colon and space
auto.fillUp(350, 10);
auto.fillUp(450, 20);
System.out.println("Miles per gallon: " + auto.calculateMPG());
auto.resetMPG();
auto.fillUp(603, 25.5);
System.out.println("Miles per gallon: " + auto.calculateMPG());
}

要支持这一点,您必须将 Getter 方法添加到您的 Car 类中,这样您就可以打印您当前的里程数:

public int getEndMiles() {
return endMiles;
}

新输出:

New car odometer reading: 15
Miles per gallon: 16.875
Miles per gallon: 16.875
Miles per gallon: 10.0
Miles per gallon: 6.0

关于java - 简单的 MPG 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32384189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com