英文:
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 < 0){
health+=shield; //shield is negative,so subtracts leftover damage from health
if(health <= 0){
lives--;
health=DEFAULT_HEALTH_VALUE; //replace with your own default values
shield=DEFAULT_SHIELD_VALUE;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论