System.out.println();
System.out.print("Enemy Pikachu did you a damage of: ");
Mine.Damage_Rec(Enemy.Tackle());
System.out.println();
System.out.println("Your Pikachu has a health of "+Mine.getHealth());
有问题的行在Enemy.Tackle()方法中。以下是:
public int Tackle() {
int damage;
if(pm1 > 0) {
damage = AttackT;
if(damage - Defense > 0) {
damage -= Defense;
System.out.println(damage);
System.out.println();
}
pm1--;
}
else {
damage = 0;
}
return damage;
}
事情是java总是计算'损坏'但有时打印出来。
如果我解释错了,请提前抱歉,我在这个领域很有竞争力;)
谢谢
答案 0 :(得分:1)
在当前代码中,print语句处于条件块中,只有在大于防御时才会打印损坏。这可以通过在Tackle()
方法的末尾打印来解决。
public int Tackle() {
int damage;
if(pm1 > 0) {
damage = AttackT;
if(damage - Defense > 0) {
damage -= Defense;
}
pm1--;
}
else {
damage = 0;
}
System.out.println(damage);
return damage;
}
甚至更好,打印Tackle
的结果:
int damage = Enemy.Tackle();
System.out.print("Enemy Pikachu did you a damage of: " + damage);
Mine.Damage_Rec(damage);