Java枚举类型错误 [英] Java Enumerated Types Error
问题描述
枚举阵营{
AMITY,ABNIGATION,DAUNTLESS,EURIDITE,CANDOR
};
我试图通过使用驱动程序来检查我的子类中的所有内容是否工作。我的子类中的构造函数看起来像这样。
public Dauntless(String f,String l,int a,int ag,int end,Faction d){
超级(f,l,a,d);
if(ag> = 0&& ag< = 10){
this.agility = ag;
} else {
this.agility = 0;
}
if(end> = 0&& end< = 10){
this.endurance = end;
} else {
this.endurance = 0;
}
}
我的驱动程序看起来像这样
public class Test {
public static void main(String [] args){
Faction this = Faction.DAUNTLESS;
无耻的乔=新的无畏(约瑟夫,胡珀,20,5,3,这个);
无畏Vik =新无畏(维多利亚,病房,19,6,2,这个);
无耻的赢家
winner = joe.battle(vik);
System.out.println(winner);
}
它一直说 Faction this = Faction.DAUNTLESS;
不是一个语句。有人可以帮助我吗?
如评论中所述, this
是Java中的关键字,用于以下内容:
this.faction;
您不能将关键字用作变量名称。只需更改变量名称:
Faction this_faction = Faction.DAUNTLESS;
然后,当然,您需要更改对变量的引用:
Dauntless joe = new Dauntless(Joseph,Hooper,20,5,3,this_faction);
无敌vik =新无畏(维多利亚,病房,19,6,2,this_faction);
so I have a base class where I define an enum variable with this block of code.
enum Faction {
AMITY, ABNIGATION, DAUNTLESS, EURIDITE, CANDOR
};
And I'm trying to test to see if everything in my subclass works by using a driver. My constructor in my subclass looks like this.
public Dauntless(String f, String l, int a, int ag, int end, Faction d) {
super(f, l, a, d);
if (ag >= 0 && ag <= 10) {
this.agility = ag;
} else {
this.agility = 0;
}
if (end >= 0 && end <= 10) {
this.endurance = end;
} else {
this.endurance = 0;
}
}
And my driver looks like this
public class Test {
public static void main(String[] args) {
Faction this = Faction.DAUNTLESS;
Dauntless joe = new Dauntless("Joseph", "Hooper", 20, 5, 3, this);
Dauntless vik = new Dauntless("Victoria", "Ward", 19, 6, 2, this);
Dauntless winner;
winner = joe.battle(vik);
System.out.println(winner);
}
It keeps saying that Faction this = Faction.DAUNTLESS;
is not a statement. Can somebody help me out here?
As mentioned in the comments, this
is a keyword in Java, used for things like:
this.faction;
You can't use keywords as variable names. Just change the variable name:
Faction this_faction = Faction.DAUNTLESS;
Then, of course, you need to change references to the variable:
Dauntless joe = new Dauntless("Joseph", "Hooper", 20, 5, 3, this_faction);
Dauntless vik = new Dauntless("Victoria", "Ward", 19, 6, 2, this_faction);
这篇关于Java枚举类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!