Java 爪哇语;此标记“后应为VariableDeclaratorId”;

Java 爪哇语;此标记“后应为VariableDeclaratorId”;,java,class,Java,Class,我正在做一个有趣的小项目,基本上是一个小的战斗模拟器。我尝试使用类似于C++中的Stult类,就像使用它创建对象(在这种情况下,一个字符或“实体”,就像这个类被调用一样)。当我试图从主函数调用所述类中的任何整数时,标题中出现了错误 class entity{ public int health; public int accuracy; public int power; public int defense; } 及 我已经四处寻找了一段时间,想找到一些解释,

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

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

我已经四处寻找了一段时间,想找到一些解释,但我找不到任何解释来解释错误的性质以及可能对我的情况进行的修复。如果我能得到这些,那就太好了。

代码导致语法错误,因为这样做是不合法的

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

这样,考虑一种类似

的方法。
// 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;
}

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

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

player.health = 100; 
而是找一份工作。声明

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

应该位于代码块(如方法或构造函数)中,而不是类块中。对于VariableDeclaratorId,这里有另一种方法会使消息传递变得非常准确,但并不可怕

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

Syntax error, insert "... VariableDeclaratorId" to complete FormalParameter
这是告诉你,是需要的变量类型,如字符串或int

 @PostMapping("/showCompany")
    public String processForm(@ModelAttribute("company") CompaniesDAO company, String company_name, String company_function) {
事实证明VariableDeclaratorId是一个类

public class VariableDeclaratorId
extends Expression
implements prettyprint.PrettyPrintable

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

您声明了一个
实体
对象,但从未初始化它。检查java中的
new
关键字。@Takendarkk编译错误可防止运行时异常;-)呼。我有一百万个打字错误:|
public class VariableDeclaratorId
extends Expression
implements prettyprint.PrettyPrintable