问题详细描述
英雄对象的属性:
姓名
最大攻击力
最小攻击力
生命值
表示攻击的方法:
攻击时随机计算攻击力(最大和最小攻击力之间的值)
方法的参数是英雄对象
代码
Hero类代码
package com.lmq.work4;
import java.util.Random;
public class Hero {
private String name;
private int maxAttack;
private int minAttack;
private int lifeValue;
public Hero(String name, int maxAttack, int minAttack, int lifeValue) {
this.name = name;
this.maxAttack = maxAttack;
this.minAttack = minAttack;
this.lifeValue = lifeValue;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMaxAttack() {
return maxAttack;
}
public void setMaxAttack(int maxAttack) {
this.maxAttack = maxAttack;
}
public int getMinAttack() {
return minAttack;
}
public void setMinAttack(int minAttack) {
this.minAttack = minAttack;
}
public int getLifeValue() {
return lifeValue;
}
public void setLifeValue(int lifeValue) {
this.lifeValue = lifeValue;
}
public void attack(Hero hero, boolean b) {
int count = 1;
System.out.print("游戏开始\t");
System.out.println("第" + count + "轮开始");
while (hero.getLifeValue() > 0 && lifeValue > 0) {
System.out.println(this.name + "拥有" + this.lifeValue + "滴血");
System.out.println(hero.name + "拥有" + hero.getLifeValue() + "滴血");
// 本体 打 传进来的英雄
Random random = new Random();
int attack = random.nextInt(maxAttack - minAttack + 1) + minAttack;
int lifeValue1 = hero.getLifeValue();
int reLifeValue = lifeValue1 - attack;
hero.setLifeValue(reLifeValue);
// 传进来的英雄 打 本体
Random random2 = new Random();
int attack2 = random.nextInt(maxAttack - minAttack + 1) + minAttack;
lifeValue = lifeValue - attack2;
if (hero.getLifeValue() > 0 && lifeValue > 0) {
System.out.println(name + "对" + hero.getName() + "攻击,产生" + attack + "滴伤害");
System.out.println(hero.getName() + "对" + name + "攻击,产生" + attack2 + "滴伤害");
count++;
System.out.print("\n游戏继续\t");
System.out.println("第" + count + "轮开始");
} else if (hero.getLifeValue() < 0 && lifeValue > 0) {
System.out.println(name + "对" + hero.getName() + "攻击,产生" + attack + "滴伤害");
System.out.println(hero.getName() + "阵亡" + this.name + "获胜");
System.out.println("游戏结束");
} else if (lifeValue < 0 && hero.getLifeValue() > 0) {
System.out.println(hero.getName() + "对" + name + "攻击,产生" + attack2 + "滴伤害");
System.out.println(name + "阵亡" + hero.getName() + "获胜");
System.out.println("游戏结束");
} else if (lifeValue < 0 && hero.getLifeValue() < 0) {
System.out.println(name + "对" + hero.getName() + "攻击,产生" + attack + "滴伤害");
System.out.println(hero.getName() + "阵亡" + this.name + "获胜");
System.out.println("游戏结束");
}
}
}
}
App测试类
package com.lmq.work4;
import java.util.Random;
public class App {
public static void main(String[] args) {
Hero hero = new Hero("程咬金", 100, 10, 200);
Hero hero2 = new Hero("诸葛亮", 100, 10, 200);
Random random = new Random();
boolean b = random.nextBoolean();
if (b) {
hero.attack(hero2, true);
} else {
hero2.attack(hero, false);
}
}
}