Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中用于计算汽车行驶里程的类_Java - Fatal编程技术网

java中用于计算汽车行驶里程的类

java中用于计算汽车行驶里程的类,java,Java,所以我正在做作业,但我总是收到一个返回值警告 加油,开车。但是,即使在添加返回语句时,我要么得到错误的答案,要么得到NaN。这是我在Java中的第一个编程类,所以我假设我的问题在于理解,所以请解释,而不是仅仅给出修复!谢谢大家! 我的汽车等级: 卡特斯特类: 将drive和addGas的回流类型从double更改为void 更改方法签名,如下所示: public void addGas(double fuel) { this.fuel = this.fuel + fuel; }

所以我正在做作业,但我总是收到一个返回值警告 加油,开车。但是,即使在添加返回语句时,我要么得到错误的答案,要么得到NaN。这是我在Java中的第一个编程类,所以我假设我的问题在于理解,所以请解释,而不是仅仅给出修复!谢谢大家!

我的汽车等级:

卡特斯特类:


将drive和addGas的回流类型从double更改为void

更改方法签名,如下所示:

public void addGas(double fuel) {
    this.fuel = this.fuel + fuel;   
}

public void drive(double distance) {
    double leftOverDistance = fuel * milesPerGallon - distance;
    this.fuel = (leftOverDistance / milesPerGallon);
}
说明: 您得到addreturnstatements错误的原因是您已经定义了返回双值的方法,但是您没有返回任何内容

在您的特定情况下,return语句没有用处。在addGas中,您正在将作为参数传递的燃油添加到this.fuel字段中,因此不返回任何内容。在驱动器中,您正在为字段this.fuel设置一个新的计算值,再次不返回任何内容。因此,void return type是这些方法的合理返回类型


顺便说一下,你的驾驶方法似乎有错误的逻辑。检查我的版本。

第一个语法错误是在驱动器和addGas中。您将返回类型设置为double,但不返回任何内容。以下是修复方法:

public void addGas(double gas){/*Code Here*/} // Void means that there won't return value
public void drive(double distance){/*Code Here*/} // Same as addGas()
我注意到的另一件事,这不是什么大问题,但我的强迫症让我不得不告诉你,当你用驱动方法从油箱中取出汽油时,你创造了一种双重命名的剩余燃料,它没有任何用途。此外,您正在将当前的燃油水平设置为MPG/距离,这是毫无意义的。您希望从当前燃油中减去所使用的燃油量。试试这个:

public void drive(double distance){
    double fuelUsed = distance / milesPerGallon;
    fuel = fuel - fuelUsed;
}

我希望这有帮助!感谢阅读。

/*您也可以使用这些类计算汽车价值。但是,此方法不会返回负值。 */


我觉得你的驾驶方法完全不对。“你应该计算一下你用了多少燃料,然后把它从燃料量中去掉。”tannar看一下我提供的答案。如果对你有帮助,点击答案旁边的空心勾号,将答案标记为已接受。当您接受回答时,您的代表性也会增加!:仔细检查你的逻辑。使用的燃油应按距离/英里计算可能不好。
public void addGas(double gas){/*Code Here*/} // Void means that there won't return value
public void drive(double distance){/*Code Here*/} // Same as addGas()
public void drive(double distance){
    double fuelUsed = distance / milesPerGallon;
    fuel = fuel - fuelUsed;
}
public class CarWorthSrv {

 public int carWorth (int year, int initVal, int mileage)
 {
    int currentYear=2016;
    final int DEP_VALUE = 1000;
    year = currentYear - year;
    double yearlyMileage = (mileage*1.0)/year;
    int value = initVal-DEP_VALUE;

    if(year==currentYear){
        value = value;
    }
    else if(year<currentYear){
        if(yearlyMileage>=10000&&yearlyMileage<=12000){
            value = value -(1400*year);
            if(value<0){
                value =0;
            }
        }
        else if(yearlyMileage>12000){
            value = value -(1550*year);
        }
        else if(yearlyMileage<10000){
            value = value -(1300*year);
        }
    }



    return value;
 }




 }
public class CarWorthDrv
 {
 public static void main(String[] args) {

    CarWorthSrv srvObj = new CarWorthSrv();

    System.out.println();
    System.out.print("Testing a 2009 vehicle.  Purchase price: $22,000 with  89,000 miles: ");
    int result = srvObj.carWorth(2009, 22000, 89000); 

    if (result == 10150)
        System.out.println("PASS");
    else
    {
        System.out.println("*** FAIL ***");
        System.out.println("should be 10150.  your method returned: " + result + "\n");
    }

    System.out.print("Testing a 2011 vehicle.  Purchase price: $29,000 with  54,500 miles: ");
    result = srvObj.carWorth(2011, 29000, 54500); 

    if (result == 21000)
        System.out.println("PASS");
    else
    {
        System.out.println("*** FAIL ***");
        System.out.println("should be 21000.  your method returned: " + result + "\n");
    }


    System.out.print("Testing a 2012 vehicle.  Purchase price: $34,400 with  24,700 miles: ");
    result = srvObj.carWorth(2012, 34400, 24700); 

    if (result == 28200)
        System.out.println("PASS");
    else
    {
        System.out.println("*** FAIL ***");
        System.out.println("should be 28200.  your method returned: " + result + "\n");
    }


    System.out.print("Testing a 2013 vehicle.  Purchase price: $32,000 with  30,000 miles: ");
    result = srvObj.carWorth(2013, 32000, 30000); 
    System.out.println( (result == 26800)? "PASS":"*** FAIL ***\nshould be 26800. Your method returned: " + result + "\n");

    System.out.print("Testing a 2014 vehicle.  Purchase price: $29,900 with  24,000 miles: ");
    result = srvObj.carWorth(2014, 29900, 24000); 
    System.out.println( (result == 26100)? "PASS":"*** FAIL ***\nshould be 26100. Your method returned: " + result + "\n");

    System.out.print("Testing a 2012 vehicle.  Purchase price: $28,900 with  48,002 miles: ");
    result = srvObj.carWorth(2012, 28900, 48002); 
    System.out.println( (result == 21700)? "PASS":"*** FAIL ***\nshould be 21700. Your method returned: " + result + "\n");

    System.out.print("Testing a 2016 vehicle.  Purchase price: $35,900 with   1,200 miles: ");
    result = srvObj.carWorth(2016, 35900, 1200); 
    System.out.println( (result == 34900)? "PASS":"*** FAIL ***\nshould be 34900. Your method returned: " + result + "\n");

    System.out.print("Testing a 1997 vehicle.  Purchase price: $19,000 with 210,000 miles: ");
    result = srvObj.carWorth(1997, 19000, 210000); 
    System.out.println( (result == 0)? "PASS":"*** FAIL ***\nshould be 0. Your method returned: " + result + "\n");

    System.out.print("Testing a 2002 vehicle.  Purchase price: $19,200 with 130,000 miles: ");
    result = srvObj.carWorth(2002, 19200, 130000); 
    System.out.println( (result == 0)? "PASS":"*** FAIL ***\nshould be 0. Your method returned: " + result + "\n");


    System.out.println();

 } //main 

 }