如何为takeDamage方法创建一个开关

huangapple go评论70阅读模式
英文:

How to create a switch for a takeDamage method

问题

我对编程有些陌生,目前正在使用Java的Netbeans。我正在尝试创建一个开关方法,根据代码中的另一个角色造成的伤害数量来消减盾牌。如果伤害超过了剩余的盾牌,就必须减掉生命值;如果没有剩余的生命值,就必须减少一条生命。生命值减少后,盾牌和生命值必须恢复为正常。我已经试了几天都没弄明白。非常感谢提供任何关于如何做到这一点的信息!

英文:

I am somewhat new to coding in general, and I am currently using Netbeans with Java. I am trying to create a switch method that takes away shield depending on how much damage is dealt with by another character in the code. If the damage takes away more than the remaining shield, health must be taken away, and if there is no health left, a life must be taken away. After life is taken, the shield and health have to return to normal. I have been trying to figure this out for days. Any info with how to do this will be GREATLY appreciated!

答案1

得分: 0

如果我正确理解了你的问题,你想要类似这样的内容:

public void takeDamage(int damage){
    shield -= damage;
    if(shield < 0){
        health += shield; //shield为负数,所以从健康值中减去多余的伤害
        if(health <= 0){
            lives--;
            health = DEFAULT_HEALTH_VALUE; //用你自己的默认值替换
            shield = DEFAULT_SHIELD_VALUE;
        }
    }
}
英文:

If I understood your question correctly, you want something like this:

public void takeDamage(int damage){
    shield-=damage;
    if(shield &lt; 0){
        health+=shield; //shield is negative,so subtracts leftover damage from health
        if(health &lt;= 0){
            lives--;
            health=DEFAULT_HEALTH_VALUE; //replace with your own default values
            shield=DEFAULT_SHIELD_VALUE;
        }
    }
}

huangapple
  • 本文由 发表于 2020年10月20日 01:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64432595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定