Java 语法错误,插入“VariableDeclarators”完成LocalVariableDeclaration 发生

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36041285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:24:05  来源:igfitidea点击:

Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration occurs

javasyntax

提问by Mike

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...
    }
}

When I tried to call distanceTraveled, I am getting the error as:

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

Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration

语法错误,插入“VariableDeclarators”完成LocalVariableDeclaration

采纳答案by mnille

To make the Syntax error disappear and to assign a value to distanceTraveledmodify the method public Water update(int time)as follows:

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

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

Maybe you should read a bit about Java and doing some tutorials, because this is very basic stuff (at least if I'm not getting you wrong).

也许你应该阅读一些关于 Java 的内容并做一些教程,因为这是非常基本的东西(至少如果我没有误解你的话)。

回答by J.M.I. MADISON

CORRECT: <%! ...code... %>(JSP DECLARATION)

正确:<%! ...代码... %>(JSP 声明)

WRONG : <% ...code... %>(JSP SCRIPLET)

错误:<% ...代码... %>(JSP 脚本)

WRONG : <%= ...code... %>(JSP EXPRESSION)

错误:<%= ...代码... %>(JSP 表达式)

Example:

例子:

<!-- ------------------------------------- -->
<html><body><h1> 
    <%!
    public static String fn(){

        return( "[CORRECT:USE ! MARK]");
    }; 
    %>

    <%= fn() %>
</h1></body></html>
<!-- ------------------------------------- -->

Using "<%"or "<%="instead of "<%!"will get error:

使用“<%”“<%=”代替“<%!” 会得到错误:

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: [3] in the jsp file: [/index.jsp] Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration

org.apache.jasper.JasperException:无法为 JSP 编译类:

在jsp文件中的第[3]行发生错误:[/index.jsp]语法错误,插入“VariableDeclarators”完成LocalVariableDeclaration