прикрепить код решения не могу, потому что задача решена. все работает. но я не понимаю,
почему private static int hitCount не инициализирована и компилятор не выдает ошибку типа error: variable ... might not have been initialized, хотя мне всегда в таких случаях выдавал. я понимаю, что присвоилось значение 0,
но в каких тогда случаях можно просто обьявлять переменную и не инициализировать.
public abstract class AbstractRobot implements Attackable, Defensable {
private static int hitCount;
public BodyPart attack() {
BodyPart attackedBodyPart = null;
hitCount = hitCount + 1;
if (hitCount == 1) {
attackedBodyPart = BodyPart.ARM;
} else if (hitCount == 2) {
attackedBodyPart = BodyPart.HEAD;
} else if (hitCount == 3) {
attackedBodyPart = BodyPart.LEG;
} else if (hitCount > 3) {
hitCount = 0;
attackedBodyPart = BodyPart.CHEST;
}
return attackedBodyPart;
}
public BodyPart defense() {
BodyPart defendedBodyPart = null;
hitCount = hitCount + 2;
if (hitCount == 1) {
defendedBodyPart = BodyPart.HEAD;
} else if (hitCount == 2) {
defendedBodyPart = BodyPart.LEG;
} else if (hitCount == 3) {
defendedBodyPart = BodyPart.ARM;
} else if (hitCount > 3) {
hitCount = 0;
defendedBodyPart = BodyPart.CHEST;
}
return defendedBodyPart;
}
}