Java运行 显示非法的表达式开始 这是为什么呀?
代码如下publicabstractclassMonster{publicStringname;publicdoublespAttackProbability=0.2;p...
代码如下public abstract class Monster{ public String name; public double spAttackProbability=0.2; public Monster(String name){ this.name=name;} public Monster(String name,double spAttackProbability){ this.name=name; this.spAttackProbability=spAttackProbability;} final public int attack(){ int X = 0; if(Math.random()<spAttackProbability) X=specialAttack(); else{ X=(int) (Math.random() * 5 + 1); System.out.println(name+",of type"+this.getClass() +",attacks generically:"+X+"points damage caused."); return X; } public abstract int specialAttack(); public void move(int direction){ switch(direction){ case 1: System.out.println(this.name+" is moving 1 step NORTH."); System.out.println(); break; case 2: System.out.println(this.name+" is moving 1 step EAST."); System.out.println(); break; case 3: System.out.println(this.name+" is moving 1 step SOUTH."); System.out.println(); break; case 4: System.out.println(this.name+" is moving 1 step WEST."); System.out.println(); break;}}}
展开
2个回答
展开全部
attack()没有返回值,我给你改了下
public abstract class Monster {
public String name;
public double spAttackProbability = 0.2;
public Monster(String name) {
this.name = name;
}
public Monster(String name, double spAttackProbability) {
this.name = name;
this.spAttackProbability = spAttackProbability;
}
final public int attack(){
int X = 0;
if(Math.random()<spAttackProbability)
X=specialAttack();
else{
X=(int) (Math.random() * 5 + 1);
System.out.println(name+",of type"+this.getClass() +",attacks generically:"+X+"points damage caused.");
return X;
}
return X;
}
public abstract int specialAttack();
public void move(int direction) {
switch (direction) {
case 1:
System.out.println(this.name + " is moving 1 step NORTH.");
System.out.println();
break;
case 2:
System.out.println(this.name + " is moving 1 step EAST.");
System.out.println();
break;
case 3:
System.out.println(this.name + " is moving 1 step SOUTH.");
System.out.println();
break;
case 4:
System.out.println(this.name + " is moving 1 step WEST.");
System.out.println();
break;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询