public class Demo01 {
public String name;//名字
public String type;//类型
public int life;//血量
public int attack;//攻击力
public int defense;//防御力
public void say(){
System.out.println("我叫"+name+",是一个"+type+",我的生命值高达"+life+",防御"+defense+",攻击"+attack);
}
public void pk(Demo01 p){
while (true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//每次都显示剩余的生命值
this.say();
p.say();
//定义一个标记,0表示我方进攻,1表示敌方进攻
int flag=0;//默认我方先进攻
if(flag==0){
int harm=this.attack-p.defense;//敌方得到的伤害
p.setLife(p.life-harm);//敌人掉血
System.out.println(p.name+"掉血" + harm);
flag = 1;//交换进攻方
}
if(flag==1){
int harm=p.attack-this.defense;//我方得到的伤害
this.setLife(this.life-harm);//我方掉血
System.out.println(this.name+"掉血" + harm);
flag = 0;//交换进攻方
}
if (this.life<=0){
System.out.println(this.name+"被KO了");
break;//有人倒下,停止战斗
}
if (p.life<=0){
System.out.println(p.name+"被KO了");
break;//有人倒下,停止战斗
}
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
}