Java 第八天
package day0506;
/**
- 奥特曼
- @author Administrator
- */
public class Ultraman {
private String name;
private int hp;
private int mp;
/**- 构造器
- @param name
*/
public Ultraman(String name){
this.name = name; // 名字
this.hp = 500; // 生命值
this.mp = 100; // 魔法值
}
/** - 普通攻击
- @param m
/
public void attack(Monster m){
int injury = (int) (Math.random()121+90);
m.setHp(m.getHp()-injury);
}
/** - 致命一击
- @param m
*/
public void hugeAttack(Monster m) {
m.setHp(m.getHp()-398);
/**- 魔法攻击
- @param ms
*/
public void magicAttack(Monster[] ms){
for (int i = 0; i < ms.length; i++) {
Monster m = ms[i];
if (m.isAlive()) {
m.setHp(m.getHp()-198);
}else {
m.setHp(0);
}
}
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp > 0? hp : 0;
}
return mp;
}
public void setMp(int mp) {
this.mp = mp;
}
@Override
public String toString() {
return name+"的生命值"+hp;
}
}
———————————————————————————————————————————————————————————————————————————————
package day0506;
/**
- 小怪兽
- @author Administrator
- */
public class Monster {
private String name;
private int hp;
/**
- 构造器
@param name
/**
*/
public Monster(String name){
this.name = name;
this.hp = 1000;
}- 普通攻击
- @param m
/
public void attack(Ultraman u){
int injury = (int) (Math.random()31+10);
u.setHp(u.getHp()-injury);
}
public int getHp() { //修改器
return hp;
}public void setHp(int hp) { //访问器
this.hp = hp > 0? hp : 0;
}public String getName() {
return name;
}public void setName(String name) {
@Override
this.name = name;
}
public String toString() {
return name+"的生命值"+hp;
}
public boolean isAlive(){
return this.hp>0;
}
}
———————————————————————————————————————————————————————————————————————————————
package day0506;
/**- 一群小怪兽群殴奥特曼
- @author Administrator
- */
public class Pk {
/**- 判断是否至少有一只小怪兽是活着的
- @param ms小怪兽的数组
@return 有活着的小怪兽返回true否则返回false
*/
public static boolean hasAliveMonster(Monster[] ms){
for (int i = 0; i < ms.length; i++) {
if (ms[i].isAlive()) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Ultraman u = new Ultraman("英雄V");
Monster[] ms = {
new Monster("僵尸A"),
new Monster("僵尸B"),
new Monster("僵尸C"),
new Monster("僵尸D"),
new Monster("僵尸E")
};int round = 1;
do {
System.out.println("=========第" + round++ + "回合=========");
Monster m = null;
do {
int mIndex = (int) (Math.random()*ms.length);
m = ms[mIndex];
} while (!m.isAlive());
System.out.println(m);
System.out.println(u);
m.attack(u);
System.out.println(m.getName()+"小怪兽攻击奥特曼");
if (u.getHp() > 0) {
u.setMp(u.getMp()+5);
double rate = Math.random();
if (rate <= 0.8) {
System.out.println("奥特曼攻击小怪兽");
u.attack(m);
}
else if (rate<=0.9) {
System.out.println("奥特曼使用致命一击");
u.hugeAttack(m);
}
else {
if (u.getMp()>=30) {
System.out.println("奥特曼使用魔法攻击");
u.magicAttack(ms);
}
else {
System.out.println("奥特曼使用魔法失败");
}
}
for (Monster tempMonster : ms) {
System.out.println(tempMonster);
}
}
} while (u.getHp() > 0 && hasAliveMonster(ms));if (u.getHp() > 0) {
System.out.println("奥特曼胜利!!");
} else {
System.out.println("小怪兽胜利!!");
}
}
}