展开全部
public class Cat {
private final String SWORD = "1";
private final String KNIFE = "2";
private Integer life = 10;// 生命值
public void setLife() {
if (!32313133353236313431303231363533e58685e5aeb931333361303062isAlive()) {
System.out.println("The cat is die!!!");
}
life -= 2;
}
public void attack(String weapon) {
if (weapon.equals(SWORD)) {
atteckedBySword();
} else if (weapon.equals(KNIFE)) {
atteckedByKnife();
}
}
public boolean isAlive() {
return life > 0;
}
public void atteckedByKnife() {
if (isAlive()) {
setLife();
System.out.println("喵喵喵");
}
}
public void atteckedBySword() {
if (isAlive()) {
setLife();
System.out.println("喵哦哦");
}
}
}public class Dog {
private final String SWORD = "1";
private final String KNIFE = "2";
private Integer life = 15;// 生命值
/**
* 设置生命值
*/
public void setLife() {
if (!isAlive()) {
System.out.println("The cat is die!!!");
}
life -= 2;// 每次受到攻击,生命值-2
}
/**
* 是否还活着
*/
public boolean isAlive() {
return life > 0;
}
public void attack(String weapon) {
if (weapon.equals(SWORD)) {
atteckedBySword();
} else if (weapon.equals(KNIFE)) {
atteckedByKnife();
}
}
/**
* 被刀攻击
*/
public void atteckedByKnife() {
if (isAlive()) {
setLife();
System.out.println("汪汪汪");
}
}
/**
* 被剑攻击
*/
public void atteckedBySword() {
if (isAlive()) {
setLife();
System.out.println("汪哦哦");
}
}
}import java.util.Scanner;
public class Game {
private static Scanner input = new Scanner(System.in);
private static Cat tomcat = new Cat();
private static Dog tomdog = new Dog();
private final static String SWORD = "1";
private final static String KNIFE = "2";
/**
* 选取武器
*
* @return 攻击武器
*/
private static String takeWeapon() {
String weapon = "1";
do {
System.out.print("\nTake your weapon(1:knife 2:sword): ");
weapon = input.next();
} while (!weapon.equals(SWORD) && !weapon.equals(KNIFE));
return weapon;
}
/**
* 随机攻击
*
* @return
*/
private static int getRandomEnemy() {
return (int) (Math.random() * 2);
}
public static void main(String[] args) {
while (true) {
if (getRandomEnemy() == 0) {// 攻击猫
System.out.println("Attack the cat ");
String weapon = takeWeapon();
tomcat.attack(weapon);
} else {
System.out.println("Attack the cat ");
String weapon = takeWeapon();
tomdog.attack(weapon);
}
if (!tomcat.isAlive() && !tomdog.isAlive()) {
break;
}
}
System.out.println("end");
}
}