package Baoji;
public class Player {
private String name;
private String type;
private int life;
private int defense;
private int attack;
public void say(){
System.out.println("我叫"+name+",是一个"+type+",我的生命值"+life+",防御"+defense+",攻击力"+attack);
}
public void pk(Player p){
int flag = 0;
while (true){
this.say();
p.say();
if (flag==0){
int harm = (this.attack-p.defense);
int sj = (int) Math.round(Math.random()*(2-1)+1);
if (sj ==2){
System.out.println(p.name+"被暴击了!掉血"+harm*2);
}else{
System.out.println(p.name+"掉血"+harm);
}
p.setLife(p.life-harm*sj);
flag = 1;
}else {
int harm = p.attack-this.defense;
int sj = (int) Math.round(Math.random()*(10-1)+1);
if (sj == 3 || sj == 9){
System.out.println(this.name+"被暴击了!掉血"+harm*2);
this.setLife(this.life-harm*2);
}else {
System.out.println(this.life+"掉血"+harm);
this.setLife(this.life-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 getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
}
package Baoji;
public class PlayerTest {
public static void main(String[] args) {
Player p1 = new Player();
p1.setName("龙");
p1.setType("战士");
p1.setLife(100);
p1.setDefense(20);
p1.setAttack(30);
Player p2 = new Player();
p2.setName("天");
p2.setType("法师");
p2.setLife(100);
p2.setDefense(15);
p2.setAttack(50);
p1.pk(p2);
}
}