好的,我的教授已经指定了
"Your ship's refuel method should take an integer amount and not return anything. When a ship is asked to refuel, its current fuel level is increased by the additional fuel amount specified and it reports that it was refueled: "Enterprise added 5 to its fuel reserves." However, if a ship is asked to refuel with an additional fuel amount less than one, the ship's fuel level remains unchanged and it reports why it wasn't refueled: "Enterprise cannot refuel with -5 fuel."
这就是我所拥有的
public refuel(int Fuels)
{
if(Fuels < 1)
{
Fuels = fuel;
"Challenger cannot refuel with " + fuel + " fuel.";
}
else
{
fuel += Fuels;
//return (name + "added " + Fuels + " to its fuel reserves");
}
}
运行此代码时出现编译错误:
invalid method declaration; return type required public refuel(int Fuels)
当我输入时
public String refuel(int Fuels) and insert the return statement, the method it compiles.
最佳答案
对于您不想返回任何内容的方法/函数,您应该使用 void 返回类型。至于说明,您的教授希望您在必要时添加燃料并向用户输出正确的消息。您的代码基本上是正确的,只有一些小的语法错误。
关于java - 难以理解方法和教授的指导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43796790/