'void' type not allowed here (java) error [closed]

淺唱寂寞╮ 提交于 2019-12-10 12:22:53

问题


Here is my code:

public void start()
{
  //If the gas tank and oil have more than nothing, and the transmission is in gear park, then the car can start
  if((gasTank.getGasLevel() > 0) && (engine.getOilLevel() > 0) && (transmission.gearPark()))
  {
    engine.startEngine();
    System.out.println("Engine is now on");
  }
  else
  {
    System.out.println("Please make sure your car is in proper gear, engine has oil and gas.");
  }
}

I keep getting 'void' type not allowed here error. I am not trying to return anything so I know I don't need int, Boolean, double, etc.

What am I doing wrong?


回答1:


Your code is not inside a class.

Try this:

public class SomeClass {
    // your method here
}



回答2:


It looks like one or more of these methods might be void, but must not be:

getGasLevel(); 
getOilLevel();
gearPark()

They must not be void because you are checking values returned from them in the if statement.



来源:https://stackoverflow.com/questions/15019723/void-type-not-allowed-here-java-error

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