HeroDemo类
package com.zhcpt.sixtest;
import java.util.Random;
public class HeroDemo {
public static void main(String[] args) {
int[] SkillHurt ={50,60,70,100};
String[] Skillcxk = {"唱","跳","Rap","篮球"};
Hero cxk = new Hero("蔡先生",Skillcxk,SkillHurt,700);
String[] Skillwyf = {"吹","拉","谈","skr"};
Hero wyf = new Hero("吴帅气",Skillwyf,SkillHurt,700);
Random random = new Random();
int i = random.nextInt(2);
System.out.println("开始对战,摇骰子决定谁先开始");
while (true){
if (i==0){
System.out.println("蔡先生先出手攻击吴帅气");
if (cxk.getHp()<=0){
System.out.println(cxk.getName()+"血量低于0,无法继续攻击"+wyf.getName()+"获胜");
break;
}
cxk.attack(wyf);
if (wyf.getHp() <= 0) {
System.out.println(wyf.getName() + "xiedi" + cxk.getName() + "获胜");
}
wyf.attack(cxk);
} else if (i == 1) {
System.out.println("吴亦凡开始攻击蔡徐坤");
if (wyf.getHp() <= 0) {
System.out.println(wyf.getName() + "血量低,无法攻击" + cxk.getName() + "获胜");
break;
}
wyf.attack(cxk);
if (cxk.getHp() <= 0) {
System.out.println(cxk.getName() + "血量为0" + wyf.getName() + "获胜");
}
cxk.attack(wyf);
}
}
}
}
Hero类
package com.zhcpt.sixtest;
import java.util.Random;
public class Hero {
private String Name;
private String[] Skill;
private int[] SkillHurts;
private double Hp;
public Hero() {
}
public Hero(String name, String[] skill, int[] skillHurts, double hp) {
Name = name;
Skill = skill;
SkillHurts = skillHurts;
Hp = hp;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String[] getSkill() {
return Skill;
}
public void setSkill(String[] skill) {
Skill = skill;
}
public int[] getSkillHurts() {
return SkillHurts;
}
public void setSkillHurts(int[] skillHurts) {
SkillHurts = skillHurts;
}
public double getHp() {
return Hp;
}
public void setHp(double hp) {
Hp = hp;
}
public void attack(Hero otherHero) {
Random random = new Random();
int SkillNum = random.nextInt(Skill.length);
String Skill = this.Skill[SkillNum];
double SkillHurt = otherHero.SkillHurts[SkillNum];
otherHero.Hp = otherHero.Hp - SkillHurt;
System.out.println(this.Name + "使用了" + Skill + "技能,对"
+ otherHero.getName() + "造成了" + SkillHurt
+ "伤害" + otherHero.getName() + "现在还剩" + otherHero.getHp() + "血量");
}
}