Error: 'void' type not allowed here

心已入冬 提交于 2019-12-02 07:17:34

The error message is telling you exactly what is wrong -- you're trying to extract a result from a method that does not return a result.

Instead, have the mileage() method return a String, not print out a String.

public String mileage() {
    return String.valueOf(miles);
}

Myself, I'd make this a getter method, and instead would do:

public int getMiles() {
    return miles;
}

Car.mileage() is void, i.e., does not return anything. It needs to return something, like in:

public int mileage() {
    return miles;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!