homework
猎人类
//猎人类
public class Hunter {
String name;//名字
int life;//生命值
String weapon;//武器
boolean isLive;//是否活着
public void fight(Monster mon) {//猎人打僵尸
System.out.print(name+"打"+mon.type+" ");
mon.injured();
}
public void injured() {//猎人受伤,生命值-10
life=life-10;
System.out.println(name+"受伤,剩余生命值为:"+life);
}
public void dead() {//猎人是否死亡
System.out.println(name+"死亡");
}
public void show() {//显示猎人状态
System.out.println(name+"存活,剩余生命值为: "+life);
}
}
僵尸类
//僵尸类
public class Monster {
String type;//类型
boolean isLive;//是否活着
int life;//生命值
public void injured() {//僵尸受伤,生命值-20
life=life-20;
System.out.println(type+"受伤,剩余生命值为:"+life);
}
public void kill(Hunter hun) {//僵尸还击
System.out.print(type+"还击"+" ");
hun.injured();//猎人受伤
}
public void dead() {//僵尸是否死亡
System.out.println(type+"死亡");
}
public void show() {//显示僵尸状态
System.out.println(type+"存活,剩余生命值为: "+life);
}
}
测试类
public class TestGame {
public static void main(String[] args) {
Hunter h=new Hunter();
h.name="奥特曼";
h.life=100;
h.weapon="M416";
h.isLive=false;
Monster m=new Monster();
m.type="小怪兽";
m.life=100;
m.isLive=false;
while(h.life>0&&m.life>0) {//都活着的时候猎人打一次僵尸,僵尸反击一次
h.fight(m);
if(m.life>0){
m.kill(h);
}
}
System.out.println('\n'+"最终状态:");
if (h.life<=0) {//猎人死
h.dead();
}else {
h.show();
}
if (m.life<=0) {//僵尸死
m.dead();
}else {
m.show();
}
}
}
结果