标签:style blog color os ar for sp div on
怪兽类
1 package com.lovo; 2 3 public class Monster { 4 private String name; 5 private int hp; //血量 6 private int aggressivity; //攻击力 7 private int defense; //防御力 8 9 10 public Monster() { 11 } 12 13 public Monster(String name, int hp, int aggressivity, int defense) { 14 this.name = name; 15 this.hp = hp; 16 this.aggressivity = aggressivity; 17 this.defense = defense; 18 } 19 /** 20 * 攻击 21 * @param ultraman 22 */ 23 public void attack(Ultraman ultraman) { 24 int currentAggressivity = (int) ((Math.random() * 20 + 81) / 100 * aggressivity); 25 int currentDefense = (int) ((Math.random() * 35 + 65) / 100 * ultraman.getDefense()); 26 if (currentAggressivity <= currentDefense) { 27 ultraman.setHp(ultraman.getHp() - 1); 28 }else { 29 ultraman.setHp(ultraman.getHp() - (currentAggressivity - currentDefense)); 30 } 31 } 32 /** 33 * 逃跑 34 * @return 35 */ 36 public String run() { 37 return "你丫的小心走夜路,奥特曼!!!"; 38 } 39 40 @Override 41 public String toString() { 42 return "name=" + name + "\nhp=" + hp + "\naggressivity=" 43 + aggressivity + "\ndefense=" + defense; 44 } 45 46 public String getName() { 47 return name; 48 } 49 public void setName(String name) { 50 this.name = name; 51 } 52 public int getHp() { 53 return hp; 54 } 55 public void setHp(int hp) { 56 if (hp < 0) { 57 this.hp = 0; 58 }else { 59 this.hp = hp; 60 } 61 } 62 public int getAggressivity() { 63 return aggressivity; 64 } 65 public void setAggressivity(int aggressivity) { 66 this.aggressivity = aggressivity; 67 } 68 public int getDefense() { 69 return defense; 70 } 71 public void setDefense(int defense) { 72 this.defense = defense; 73 } 74 75 76 }
奥特曼类
1 package com.lovo; 2 3 public class Ultraman { 4 private String name; 5 private int hp; 6 private int aggressivity; 7 private int defense; 8 private int energy; //能量,放技能 9 private boolean isShapeShift = false; 10 11 12 public Ultraman() { 13 } 14 15 16 public Ultraman(String name, int hp, int aggressivity, int defense, 17 int energy) { 18 this.name = name; 19 this.hp = hp; 20 this.aggressivity = aggressivity; 21 this.defense = defense; 22 this.energy = energy; 23 } 24 25 26 /** 27 * 奥特曼攻击 28 * @param monster 29 */ 30 public void attack(Monster monster) { 31 int currentAggressivity = (int) ((Math.random() * 20 + 81) / 100 * aggressivity); 32 int currentDefense = (int) ((Math.random() * 35 + 65) / 100 * monster.getDefense()); 33 if (currentAggressivity <= currentDefense) { 34 monster.setHp(monster.getHp() - 1); 35 }else { 36 monster.setHp(monster.getHp() - (currentAggressivity - currentDefense)); 37 } 38 } 39 /** 40 * 变身前的大招 41 * @param monster 42 * @return 43 */ 44 public boolean radiateUlti(Monster monster) { 45 if (!isShapeShift) { 46 if (energy > 0) { 47 monster.setHp(monster.getHp() - energy * 3); 48 energy = 0; 49 return true; 50 } 51 } 52 return false; 53 54 } 55 /** 56 * 变身后的大招 57 * @param monsters 58 * @return 59 */ 60 public boolean radiateUlti(Monster[] monsters) { 61 if (isShapeShift) { 62 if (energy > 0) { 63 for (Monster monster : monsters) { 64 monster.setHp(monster.getHp() - energy * 3); 65 } 66 energy = 0; 67 return true; 68 } 69 } 70 return false; 71 } 72 /** 73 * 逃跑 74 * @return 75 */ 76 public String run() { 77 return "我还会回来的!!!"; 78 } 79 80 81 @Override 82 public String toString() { 83 return "name=" + name + "\nhp=" + hp + "\naggressivity=" 84 + aggressivity + "\ndefense=" + defense + "\nenergy=" + energy 85 + "\nisShapeShift=" + isShapeShift; 86 } 87 88 public String getName() { 89 return name; 90 } 91 92 public void setName(String name) { 93 this.name = name; 94 } 95 96 public int getHp() { 97 return hp; 98 } 99 100 public void setHp(int hp) { 101 if (hp < 0) { 102 this.hp = 0; 103 }else { 104 this.hp = hp; 105 } 106 } 107 108 public int getAggressivity() { 109 return aggressivity; 110 } 111 112 public void setAggressivity(int aggressivity) { 113 this.aggressivity = aggressivity; 114 } 115 116 public int getDefense() { 117 return defense; 118 } 119 120 public void setDefense(int defense) { 121 this.defense = defense; 122 } 123 124 public int getEnergy() { 125 return energy; 126 } 127 128 public void setEnergy(int energy) { 129 this.energy = energy; 130 } 131 132 public boolean isShapeShift() { 133 return isShapeShift; 134 } 135 136 public void setShapeShift(boolean isShapeShift) { 137 this.isShapeShift = isShapeShift; 138 } 139 140 }
测试类
奥特曼打一只怪兽
1 public static void main(String[] args) { 2 //一只怪兽 3 Ultraman u1 = new Ultraman("泰罗", 700, 100, 80, 150); 4 Monster m1 = new Monster("佐利姆", 600, 130, 100); 5 System.out.println("奥特曼初始状态:\n" + u1); 6 System.out.println("怪兽初始状态:\n" + m1); 7 for (int i=1;u1.getHp() > 0 && m1.getHp() > 0; i++) { 8 System.out.println("第"+i+"回合"); 9 int beforeMHp = m1.getHp(); 10 if (u1.getHp() >= m1.getAggressivity() - u1.getDefense() * 0.65) { 11 u1.attack(m1); 12 }else { 13 u1.radiateUlti(m1); 14 } 15 System.out.println("怪兽受到"+ (beforeMHp - m1.getHp()) +"点伤害。"); 16 if (m1.getHp() > 0) { 17 int beforeUHp = u1.getHp(); 18 m1.attack(u1); 19 System.out.println("奥特曼受到"+ (beforeUHp - u1.getHp()) +"点伤害。"); 20 } 21 } 22 System.out.println("奥特曼最后状态:\n" + u1); 23 System.out.println("怪兽最后状态:\n" + m1); 24 if (u1.getHp() > 0 && m1.getHp() <= 0) { 25 System.out.println(u1.getName() + "胜利!"); 26 } 27 else if (m1.getHp() > 0 && u1.getHp() <= 0) { 28 System.out.println(m1.getName() + "胜利!"); 29 }
30 }
奥特曼打多只小怪兽
1 public static void main(String[] args) { 2 Ultraman u1 = new Ultraman("泰罗", 900, 140, 80, 150); 3 Monster m1 = new Monster("佐利姆", 750, 130, 100); 4 Monster m2 = new Monster("伊特", 550, 90, 80); 5 Monster m3 = new Monster("卡丹", 400, 140, 130); 6 Monster[] ms = {m1,m2,m3}; 7 int monsterNum = ms.length; 8 boolean goon = true; 9 System.out.println("奥特曼初始状态:\n" + u1); 10 System.out.println("怪兽初始状态:\n" + m1 +"\n------------------" +"\n" + m2 +"\n------------------" +"\n" + m3); 11 for (int i=1;u1.getHp() > 0 && monsterNum > 0 && goon; i++) { 12 System.out.println("第"+i+"回合"); 13 if (u1.getHp() > (m1.getAggressivity() + m2.getAggressivity() + m3.getAggressivity()-u1.getDefense() * 0.65 * 3) || u1.getEnergy() == 0) { 14 int beforeMHp = 0; 15 for (int j = 0; j < ms.length; j++) { 16 beforeMHp = ms[j].getHp(); 17 if (beforeMHp > 0) { 18 u1.attack(ms[j]); 19 System.out.println(ms[j].getName() + "怪兽受到"+ (beforeMHp - ms[j].getHp()) +"点伤害。"); 20 if (ms[j].getHp() <= 0) { 21 monsterNum--; 22 System.out.println(ms[j].getName() + "怪兽死亡!"); 23 } 24 } 25 } 26 if (u1.getHp() < 100 && u1.getEnergy() == 0 && monsterNum != 0) { 27 System.out.println(u1.run()); 28 goon = false; 29 System.out.println("怪兽胜利!"); 30 } 31 }else { 32 int[] beforeMHp = new int[ms.length]; 33 for (int j = 0; j < beforeMHp.length; j++) { 34 beforeMHp[j] = ms[j].getHp(); 35 } 36 u1.setShapeShift(true); 37 u1.radiateUlti(ms); 38 for (int j = 0; j < ms.length; j++) { 39 if (beforeMHp[j] > 0) { 40 System.out.println(ms[j].getName() + "怪兽受到"+ (beforeMHp[j] - ms[j].getHp()) +"点伤害。"); 41 if (ms[j].getHp() <= 0) { 42 monsterNum--; 43 System.out.println(ms[j].getName() + "怪兽死亡!"); 44 } 45 } 46 } 47 } 48 for (int j = 0; j < ms.length && goon; j++) { 49 if (ms[j].getHp() > 0) { 50 int beforeUHp = u1.getHp(); 51 m1.attack(u1); 52 System.out.println("奥特曼受到来至"+ ms[j].getName() + "的" + (beforeUHp - u1.getHp()) +"点伤害。"); 53 if (monsterNum == 1 && ms[j].getHp() < 100 && u1.getHp() > 0) { 54 System.out.println(ms[j].run()); 55 goon =false; 56 System.out.println(u1.getName() + "胜利!"); 57 } 58 } 59 } 60 } 61 System.out.println("奥特曼最后状态:\n" + u1); 62 System.out.println("怪兽最后状态:\n" + m1 +"\n------------------" +"\n" + m2 +"\n------------------" +"\n" + m3); 63 if (u1.getHp() > 0 && m1.getHp() <= 0 && m2.getHp() <= 0 && m3.getHp() <= 0) { 64 System.out.println(u1.getName() + "胜利!"); 65 } 66 else if ((m1.getHp() > 0 || m2.getHp() > 0 || m3.getHp() > 0) && u1.getHp() <= 0) { 67 System.out.println("怪兽胜利!"); 68 } 69 }
标签:style blog color os ar for sp div on
原文地址:http://www.cnblogs.com/f644135318/p/4051429.html