Java 计算 MPG 误差
Java Calculating MPG Error
我在这个作业中遇到了问题,我应该计算某个输入变量的每加仑英里数。
作业的规格是:http://www.mrferrante.com/apcs/WebLessons/LessonA4/images/Lab_A1_fig_1.gif
我应该计算 MPG
这是我的 Driver:
public class Driver{
public static void main(String[] args){
int odometerReading = 15;
P4_Icel_Murad_Car auto = new P4_Icel_Murad_Car(odometerReading);
System.out.println("New car odometer reading: " + startMiles);
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());
}
}
下面是代码的其余部分:
public class P4_Icel_Murad_Car{
private int myStartMiles;
private int myEndMiles;
private double myGallonsUsed;
public int P4_Icel_Murad_Car(int odometerReading){
myStartMiles = odometerReading;
return myStartMiles;
}
public void fillUp(int odometerReading,double gallons){
int Miles = odometerReading - myStartMiles;
double MilesPerGallon = Miles / gallons;
}
public double calculateMPG( int odometerReading, double gallons){
int MPG = (int)(odometerReading/gallons);
return MPG;
}
public void resetMPG(){
myStartMiles = 0;
myEndMiles = 0;
myGallonsUsed = 0;
}
}
当输入15作为startMiles时,结果应该是
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
但是我正在使用的 IDE (BlueJ) 声称 P4_Icel_Murad_Car() 不需要任何参数并且我正在放入 int 而不是什么都没有。
提前感谢您的帮助!
你的 class 叫做 P4_Icel_Murad_Car
。
所以 P4_Icel_Murad_Car
应该是 class 的构造函数,你不能从它 return 一个 int
。
尝试将该方法替换为:
public P4_Icel_Murad_Car(int odometerReading){
myStartMiles = odometerReading;
}
你的代码有几个错误,这里是带注释的工作版本:
public static class P4_Icel_Murad_Car {
private int myStartMiles;
private int myEndMiles;
private double myGallonsUsed;
public P4_Icel_Murad_Car(final int odometerReading) { // Constructor does not have return type, do not put int before constructor name
this.myStartMiles = odometerReading;
this.myEndMiles = odometerReading; // init end miles with start miles from the start
this.myGallonsUsed = 0;
}
public void fillUp(final int odometerReading, final double gallons) {
this.myEndMiles = odometerReading; // store current odometer
this.myGallonsUsed += gallons; // sum up gas
}
public double calculateMPG() { // calculate mileage, divide by gallons
return (this.myEndMiles - this.myStartMiles) / this.myGallonsUsed;
}
public void resetMPG() { // reset means start from current odometer reading
this.myStartMiles = this.myEndMiles;
this.myGallonsUsed = 0;
}
}
我在这个作业中遇到了问题,我应该计算某个输入变量的每加仑英里数。
作业的规格是:http://www.mrferrante.com/apcs/WebLessons/LessonA4/images/Lab_A1_fig_1.gif
我应该计算 MPG
这是我的 Driver:
public class Driver{
public static void main(String[] args){
int odometerReading = 15;
P4_Icel_Murad_Car auto = new P4_Icel_Murad_Car(odometerReading);
System.out.println("New car odometer reading: " + startMiles);
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());
}
}
下面是代码的其余部分:
public class P4_Icel_Murad_Car{
private int myStartMiles;
private int myEndMiles;
private double myGallonsUsed;
public int P4_Icel_Murad_Car(int odometerReading){
myStartMiles = odometerReading;
return myStartMiles;
}
public void fillUp(int odometerReading,double gallons){
int Miles = odometerReading - myStartMiles;
double MilesPerGallon = Miles / gallons;
}
public double calculateMPG( int odometerReading, double gallons){
int MPG = (int)(odometerReading/gallons);
return MPG;
}
public void resetMPG(){
myStartMiles = 0;
myEndMiles = 0;
myGallonsUsed = 0;
}
}
当输入15作为startMiles时,结果应该是
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
但是我正在使用的 IDE (BlueJ) 声称 P4_Icel_Murad_Car() 不需要任何参数并且我正在放入 int 而不是什么都没有。 提前感谢您的帮助!
你的 class 叫做 P4_Icel_Murad_Car
。
所以 P4_Icel_Murad_Car
应该是 class 的构造函数,你不能从它 return 一个 int
。
尝试将该方法替换为:
public P4_Icel_Murad_Car(int odometerReading){
myStartMiles = odometerReading;
}
你的代码有几个错误,这里是带注释的工作版本:
public static class P4_Icel_Murad_Car {
private int myStartMiles;
private int myEndMiles;
private double myGallonsUsed;
public P4_Icel_Murad_Car(final int odometerReading) { // Constructor does not have return type, do not put int before constructor name
this.myStartMiles = odometerReading;
this.myEndMiles = odometerReading; // init end miles with start miles from the start
this.myGallonsUsed = 0;
}
public void fillUp(final int odometerReading, final double gallons) {
this.myEndMiles = odometerReading; // store current odometer
this.myGallonsUsed += gallons; // sum up gas
}
public double calculateMPG() { // calculate mileage, divide by gallons
return (this.myEndMiles - this.myStartMiles) / this.myGallonsUsed;
}
public void resetMPG() { // reset means start from current odometer reading
this.myStartMiles = this.myEndMiles;
this.myGallonsUsed = 0;
}
}