java - 出现语法错误,插入 "VariableDeclarators"来完成 LocalVariableDeclaration

标签 java syntax

public class Water {
    private Graphic graphic;
    private float speed;
    private float distanceTraveled;

    public Water(float x, float y, float direction) 
    {
        speed = 0.7f;

        graphic = new Graphic();
        graphic.setType("WATER");   

        graphic.setX(x);
        graphic.setY(y);

        direction = graphic.getDirection(); //direction from Hero as water is fired
    }
    public Water update(int time) 
    {
        graphic.draw();
        return Water.this;
        distanceTraveled; // this is where the error occured...
    }
}

当我尝试调用 distanceTraveled 时,收到错误如下:

Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration

最佳答案

要使语法错误消失并为 distanceTraveled 赋值,请修改方法 public Water update(int time),如下所示:

public Water update(int time) {
    graphic.draw();
    distanceTraveled = 1; // assign a value before returning
    return Water.this;
}

也许您应该阅读一些有关 Java 的内容并学习一些教程,因为这是非常基本的内容(至少如果我没有理解错的话)。

关于java - 出现语法错误,插入 "VariableDeclarators"来完成 LocalVariableDeclaration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36041285/

相关文章:

java - 使用数据库测试 spring boot api

java - 如何获取 Restheart 系列的 Etag

javascript - ReactJS 中的内联和映射的语法错误/jshint

jquery - 引号内的jQuery变量返回语法错误

.net - 什么是.NET中的页面指令

java - 无法向我的 JFrame 添加面板(背景)

java - 告诉 jaxb 不要删除下划线

java - 导入maven依赖

python - While 1 与 While True。哪一个更Pythonic?

javascript - 为什么 10..toString() 有效,但 10.toString() 无效?