Java "VariableDeclaratorId expected after this token"

标签 java class

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

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

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

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

最佳答案

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

player.health = 100; 

而是在找工作。声明

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

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

关于Java "VariableDeclaratorId expected after this token",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22622676/

相关文章:

Ruby 类属性

python - 什么方法在 Python 类中调用 `__init__()`

c++ - 有没有办法禁用非动态类构造函数?

C++ 类重载运算符

java - JFreeChart 简单绘图(抛物线)

java - Android contentprovider 不返回 Phone.NUMBER

java - JASIG CAS : How do I change where log files are written to?

java - 一般调试日志实践

java - 如果只在对象初始化时分配内存,类成员变量(实例变量)如何初始化?

Java 删除 JTree 中 DefaultMutableTreenNode 上的 focusPainted