1ly45
7 уровень

Cat fighting game

Статья из группы Архив info.javarush
Приложуху на мобилку, где будет идти сравнение подобных величин public static void main(String[] args) { Cat irbis = new Cat("Irbis", 2,5,6); Cat puskin = new Cat("Pushkin", 5,4,4); Cat margo = new Cat("Margo", 4,3,3); cat1.fight(cat2); cat1.fight(cat3); cat2.fight(cat3); } public static class Cat { private String name; //protected instead private private int age; private int weight; private int strength; public Cat(String name, int age, int weight, int strength) { this.name = name; this.age = age; this.weight = weight; this.strength = strength; } } public boolean fight(Cat anotherCat) { int power1 = this.strength + this.weight; int power2 = anotherCat.strength + anotherCat.weight; if (this.age >=3 && this.age <= 8) { power1 = power1 + this.age; } else if(this.age<3) { power1=power1-(8-this.age); } else { power1=power1-4; } if (anotherCat.age >=3 && anotherCat.age <= 8) { power2 = power2 + anotherCat.age; } else if(anotherCat.age<3) { power2=power2-(8-anotherCat.age); } else { power2=power2-4; } boolean result = power1 > power2; return result; } /* public boolean fight(Cat anotherCat) { int agePlus = this.age > anotherCat.age ? 1 : 0; int weightPlus = this.weight > anotherCat.weight ? 1 : 0; int strengthPlus = this.strength > anotherCat.strength ? 1 : 0; int score = agePlus + weightPlus + strengthPlus; return score > 2; // return score > 2 ? true : false; } */
Комментарии
ЧТОБЫ ПОСМОТРЕТЬ ВСЕ КОММЕНТАРИИ ИЛИ ОСТАВИТЬ КОММЕНТАРИЙ,
ПЕРЕЙДИТЕ В ПОЛНУЮ ВЕРСИЮ