package zh.codegym.task.task05.task0505;
/*
猫类大屠杀
*/
public class Solution {
public static void main(String[] args) {
Cat cat1 = new Cat("cat1", 21, 16, 50);
Cat cat2 = new Cat("cat2", 15, 20, 20);
Cat cat3 = new Cat("cat3", 20, 12, 35);
cat1.fight(cat2);
if (cat1.fight(cat2) == true) {
System.out.println("cat1赢");
} else {
System.out.println("cat2赢");
}
cat1.fight(cat3);
if (cat1.fight(cat3) == true) {
System.out.println("cat1赢");
} else {
System.out.println("cat3赢");
}
cat2.fight(cat3);
if (cat2.fight(cat3) == true) {
System.out.println("cat2赢");
} else {
System.out.println("cat3赢");
}
}
public static class Cat {
protected String name;
protected int age;
protected int weight;
protected 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 ageAdvantage = this.age > anotherCat.age ? 1 : 0;
int weightAdvantage = this.weight > anotherCat.weight ? 1 : 0;
int strengthAdvantage = this.strength > anotherCat.strength ? 1 : 0;
int score = ageAdvantage + weightAdvantage + strengthAdvantage;
return score > 2; // return score > 2 ? true : false;
}
}
}
package zh.codegym.task.task05.task0505;
/*
猫类大屠杀
*/
public class Solution {
public static void main(String[] args) {
Cat cat1 = new Cat("cat1", 21, 16, 50);
Cat cat2 = new Cat("cat2", 15, 20, 20);
Cat cat3 = new Cat("cat3", 20, 12, 35);
cat1.fight(cat2);
if (cat1.fight(cat2) == true) {
System.out.println("cat1赢");
} else {
System.out.println("cat2赢");
}
cat1.fight(cat3);
if (cat1.fight(cat3) == true) {
System.out.println("cat1赢");
} else {
System.out.println("cat3赢");
}
cat2.fight(cat3);
if (cat2.fight(cat3) == true) {
System.out.println("cat2赢");
} else {
System.out.println("cat3赢");
}
}
public static class Cat {
protected String name;
protected int age;
protected int weight;
protected 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 ageAdvantage = this.age > anotherCat.age ? 1 : 0;
int weightAdvantage = this.weight > anotherCat.weight ? 1 : 0;
int strengthAdvantage = this.strength > anotherCat.strength ? 1 : 0;
int score = ageAdvantage + weightAdvantage + strengthAdvantage;
return score > 2; // return score > 2 ? true : false;
}
}
}