我现在正在学习Java,我有一个关于一个对象的问题,这个对象在程序执行期间被初始化并更改了一个变量.
public class Char {
private String name;
private int skill;
private int defense;
private int life;
private Weapon weapon = Weapon.FISTS;
private Potion potion = null;
所以,我想让这段代码获得被初始化的生命的初始值,但我如何访问它呢?
public boolean isWeak() {
return life < this.life * 0.25;
}
因此,该方法位于Char类中.我正在try 让它在低于25%时返回真值.
while (hero.isAlive() && monster.isAlive()) {
if (hero.isWeak() && hero.hasPotion()) {
hero.sip();
} else if (monster.isWeak() && monster.hasPotion()){
monster.sip();
} else {
System.out.println(monster.isWeak());
hero.attack(monster);
if (monster.isAlive()) {
monster.attack(hero);
}
System.out.println();
}
}
这是执行程序.所有其他方法都工作得很好,但正如所指出的,它永远不会返回TRUE,因为它不能是本身的四分之一.别介意 fingerprint ,我只是在测试它.