问题描述
我正在做一个有趣的小项目,它本质上是一个战斗模拟器。我正在尝试使用类似于C ++中struct的类,就像使用它来创建对象一样(在这种情况下,称为字符或实体,因为该类被称为)。尝试从主函数调用所述类中的任何整数时,标题出现错误。
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.
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...
我已经看了一段时间了找到一些解释,但是我找不到能解释该错误的性质以及针对我的情况的可能解决方法的任何信息。如果我能得到这些,那就太好了。
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.
推荐答案
编译器期望在线上进行变量声明
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
这篇关于Java“在此令牌之后期望的VarialableDeclaratorId”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!