java - 难以理解方法和教授的指导

标签 java methods

好的,我的教授已经指定了

"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/

相关文章:

java - Vaadin 中图表配置的序列化

java - 如何在 Android TextView 中有 "&"即和符号

java - 第二次无法从用户处获取字符串输入的输入

iphone - 继续执行方法,即使它的 ViewController 在 iOS 中消失

java - 方法中的通用参数 - Java

java - JSP 中调用 session 属性

java - 按类型 Autowiring 和按名称 Autowiring 之间有性能差异吗

java - 如何获取二维数组的众数

java - java运行时的方法声明

java - 在什么情况下静态方法是一个好习惯?