Java“此令牌后预期的VariableDeclaratorId”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22622676/
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-13 16:55:12  来源:igfitidea点击:

Java "VariableDeclaratorId expected after this token"

javaclass

提问by user2895469

I'm working on a little project for fun, that's essentially a little combat emulator. I'm trying to use a class similar to struct in C++, as in using it to create an object (in this case, a character or "entity", as the class is called). I'm getting the error in the title when attempting to call any integer in said class from the main function.

我正在做一个有趣的小项目,它本质上是一个小战斗模拟器。我试图在 C++ 中使用一个类似于 struct 的类,就像使用它来创建一个对象一样(在这种情况下,一个字符或“实体”,因为类被称为)。尝试从主函数调用所述类中的任何整数时,标题中出现错误。

class entity{
    public int health;
    public int accuracy;
    public int power;
    public int defense;
}

And

public class Tutorial {
    static Random rnd = new Random();
    entity player;
    player.health = 100;  // Issue on the health part
    player.accuracy = 19; // Issue on the accuracy part
    player.power = 15;    // Issue on the power part
    player.defense = 18;  // I think you get it by now...

I've been looking around for a while to find some explanation, but there's none that I could find that explain the nature of the error as well as possible fixes for my situation. If I could get those, it would be wonderful.

我已经环顾了一段时间以找到一些解释,但是我找不到任何解释错误的性质以及针对我的情况的可能修复方法。如果我能得到这些,那就太好了。

回答by user2864740

Procedural code cannotbe written directly in a class definition.The code is causing syntax errorsbecause it's not legal to do so.

过程代码不能直接写在类定义中。该代码导致语法错误,因为这样做是不合法的。

Instead, put the code in an appropriate methodor initialization block. (I do notthink an initialization block is appropriate here, so I am trivially showing a "factory method".)

相反,将代码放在适当的方法初始化块中。(我认为这里适合使用初始化块,所以我只是简单地展示了一个“工厂方法”。)

As such, consider an approach like

因此,考虑一种方法,如

// This is a member variable declaration, which is why it's OK
// to have a (static) method call provide the value.
// Alternatively, player could also be initialized in the constructor.
Entity player = makeMeAPlayer();

static Entity makeMeAPlayer() {
    // Create and return entity; the code is inside a method
    Entity player = new Entity();
    player.health = 100;
    // etc.
    return player;
}

(I've also cleaned up the type to match Java Naming Conventions - follow suite!)

(我还清理了类型以匹配 Java 命名约定 - 跟随套件!)

回答by Reimeus

The compiler is expecting a variable declaration on the line

编译器期望在行上有一个变量声明

player.health = 100; 

but is finding an assignment instead. The statements

但正在寻找任务。声明

Entity player = new Entity();
player.health = 100; 
player.accuracy = 19; 
player.power = 15; 
player.defense = 18; 

should be in a code block such as a method or constructor rather than the class block

应该在诸如方法或构造函数之类的代码块中,而不是在类块中

回答by Richard Bradley Smith

Here is another way to suffer from highly accurate yet not terrible intuitive messaging for VariableDeclaratorId.

这是 VariableDeclaratorId 遭受高度准确但并不可怕的直观消息传递的另一种方式。

 @PostMapping("/showCompany")
    public String processForm(@ModelAttribute("company") CompaniesDAO company, company_name, company_function) {

Will produce:

将产生:

Syntax error, insert "... VariableDeclaratorId" to complete FormalParameter

This is telling you that is need the variable type like String or int...

这告诉你需要像 String 或 int 这样的变量类型......

 @PostMapping("/showCompany")
    public String processForm(@ModelAttribute("company") CompaniesDAO company, String company_name, String company_function) {

It turns out VariableDeclaratorId is a class

原来 VariableDeclaratorId 是一个类

public class VariableDeclaratorId
extends Expression
implements prettyprint.PrettyPrintable

If you read the class description using it with the error message to determine that you forgot to define the variable type, I am impressed; I simply made an educated guess.

如果您阅读使用它的类描述和错误消息以确定您忘记定义变量类型,我印象深刻;我只是做了一个有根据的猜测。