由于奥特曼和小怪兽有相同的属性,我把他们化成一个类,怪物。通过这个类的调用,可以实现奥特曼与小怪兽的打斗。
package com.lovo;
/**
* 这是写的奥特曼和小怪兽的共同类,由于他们共有相同的属性,行为,所以归为一类。
* @author 杜海
*
*/
public class Guaiwu {
private String name;
private int hp; //生命值
private int mp; // 魔法值
private int demage; //伤害值
private int defense; //防御值
private int magicdemage; //魔法伤害
private int everymagic; //每次放魔法伤害需要的魔法值
public Guaiwu(String name,int hp,int mp,int demage,int defense,int magicdemage,int everymagic){
this.name=name;
this.hp=hp;
this.mp=mp;
this.demage=demage;
this.defense=defense;
this.magicdemage=magicdemage;
this.everymagic=everymagic;
}
public void attack( Guaiwu gs){
int now_demage=(int) (Math.random()*demage/2+demage/2); //普通攻击值
int gs_defense=(int)(gs.defense*(Math.random()*50+50)/100);//奥特曼或者小怪兽的防御
if (now_demage - gs.defense <= 0) {
gs.hp -= 1;
} else
gs.hp -= now_demage - gs.defense;
if(gs.hp<=0)
gs.hp=0;
}
public void magicattack(Guaiwu gs){
if (mp>=everymagic) {
int now_demage = (int) (Math.random() * magicdemage / 2 + magicdemage / 2);//魔法伤害
int gs_defense = (int) (gs.defense * (Math.random() * 50 + 50) / 100);
mp -= everymagic;
if (now_demage - gs.defense <= 0) {
gs.hp -= 1;
} else
gs.hp -= now_demage - gs.defense;
if (gs.hp <= 0)
gs.hp = 0;
}
}
public boolean isAlive(){ //判断怪物是否活的
return this.hp>0;
}
public String getname(){
return this.name;
}
public String getinfo(){
return "姓名:"+name+"\n生命:"+hp+"\n魔法:"+mp+"\n魔法伤害"+magicdemage+"\n攻击力:"+demage+"\n防御力:"+defense;
}
}
package com.lovo;
/**
* 本程序是写的奥特曼打小怪的程序,其中奥特曼同时打3个小怪兽
* @author 杜海
*
*/
public class Test_0001 {
public static void main(String[] args) {
Guaiwu aot = new Guaiwu("奥特曼", 4000, 1000, 200, 88, 600,50);
Guaiwu[] gs1={new Guaiwu("小怪兽1号", 1000, 3000, 100, 30, 200,50),new Guaiwu("小怪兽2号", 1000, 3000, 100, 30, 200,50),new Guaiwu("小怪兽3号", 1000, 3000, 100, 30, 200,50)};
int round = 1;
while (gs1[0].isAlive()||gs1[1].isAlive()||gs1[2].isAlive() ) { //奥特曼打3个怪兽
System.out.println("第" + round + "回合!");
if (gs1[0].isAlive()) {
if (round % 4 != 0) { //每4回合,释放一次魔法攻击
aot.attack(gs1[0]);
if (gs1[0].isAlive()) {
gs1[0].attack(aot);
}
} else {
System.out.println("本次放魔法技能");
aot.magicattack(gs1[0]);
if (gs1[0].isAlive()) {
gs1[0].magicattack(aot);
}
}
System.out.println(aot.getinfo());
System.out.println(gs1[0].getinfo());
System.out.println("\n");
}
if (gs1[1].isAlive()) {
if (round % 4 != 0) {
aot.attack(gs1[1]);
if (gs1[1].isAlive()) {
gs1[1].attack(aot);
}
} else {
System.out.println("本次放魔法技能");
aot.magicattack(gs1[1]);
if (gs1[1].isAlive()) {
gs1[1].magicattack(aot);
}
}
System.out.println(aot.getinfo());
System.out.println(gs1[1].getinfo());
System.out.println("\n");
}if (gs1[2].isAlive()) {
if (round % 4 != 0) {
aot.attack(gs1[2]);
if (gs1[2].isAlive()) {
gs1[2].attack(aot);
}
} else {
System.out.println("本次放魔法技能");
aot.magicattack(gs1[2]);
if (gs1[2].isAlive()) {
gs1[2].magicattack(aot);
}
}
System.out.println(aot.getinfo());
System.out.println(gs1[2].getinfo());
System.out.println("\n");
}
round++;
}
if (aot.isAlive()) {
System.out.println(aot.getname() + "打死了所有的小怪兽!");
} else if (gs1[0].isAlive()) {
System.out.println(gs1[0].getname() + "打死了奥特曼!");
}
else if (gs1[1].isAlive()) {
System.out.println(gs1[1].getname() + "打死了奥特曼!");
}
else if (gs1[0].isAlive()) {
System.out.println(gs1[2].getname() + "打死了奥特曼!");
}
}
}