java非法表达式开头,java - 我不断收到“ Battle.java:11:错误:表达式的非法开头” Heelp - SO中文参考 - www.soinside.com...

问题之一是,您为函数“ fightToDeath”提供了两个类型为“ monster”的对象。到目前为止还可以。但是这里变得非常错误:int m1 = s1;

您将一个怪物对象(已全局声明且不是自变量)赋予一个整数。我不知道您要达到什么目标,但这可能对您来说是正确的:int m1Strength = m1.getStrength();

int m2Strength = m2.getStrength();

在您的代码中,您在“ fightToDeath”中的参数将立即被覆盖。按照我写的方式,您将使用您给出的怪物作为参数,并获得它们的强度,然后将此强度分配给局部整数。

编辑:

查看此代码:public class Battle{

public static void main (String [] args){

Monster s1 = new Monster("Demorgogon", 10);

Monster s2 = new Monster("Godzilla", 98);

fightToDeath(s1, s2);

}

public static void fightToDeath(Monster m1, Monster m2){

int m1Strength = m1.getStrength();

int m2Strength = m2.getStrength();

int attack = 5;

int attack2 = 2;

while (m1Strength > 0 && m2Strength > 0) {

m1.oof(attack2);

m1Strength -= attack2;

m2.oof(attack);

m2Strength -= attack;

}

if(m1.getStrength() == 0){

System.out.println("Winner is: " + m2.getName() + " with " +

m2.getStrength() + " live left!");

} else {

System.out.println("Winner is: " + m1.getName() + " with " +

m1.getStrength() + " live left!");

}

}

}

public class Monster{

int strength;

String name;

public Monster(String name, int strength){

this.name = name;

this.strength = strength;

}

public void setStrength(int newStrength){

this.strength = newStrength;

}

public int getStrength(){

return this.strength;

}

public void setName(String newName) {

this.name = newName;

}

public String getName(){

return this.name;

}

public void oof(int attackValue){

this.strength -= attackValue;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值