如果有人可以给我一些指导或解决方案,那将是很好的。我正在尝试让代码运行汽车mpg计算器
当我运行代码时,出现如下错误:
U:\gasProj.java:32: error: incompatible types: void cannot be converted to double
fuelLeft=bmw.takeTrip(100);
这是我的代码。
public class gasProj{
public static class Automobile{
double mpg=0;
Automobile(double a)
{
mpg=a;
}
private double fuel=0.0;
public void fillup(double gas)
{
fuel=fuel+gas;
}
public void takeTrip(double miles)
{
fuel=fuel-miles/mpg;
}
public double printFuel()
{
System.out.println(fuel);
}
}
public static void main(String[]args){
Automobile bmw=new Automobile(24);
bmw.fillup(20);
double fuelLeft;
fuelLeft=bmw.takeTrip(100);
}
}
您需要更改takeTrip
为:
public double takeTrip(double miles)
{
fuel=fuel-miles/mpg;
return fuel;
}
当前,takeTrip
不返回任何内容,因此您无法将返回值分配给double。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句