javaBean 主体代码
import java.util.Random;
public class Role {
private String name;
private int blood;
//路飞的技能
String[] attacks_lufei = {"%s使用了橡胶巨人手枪攻击了%s", "%s使用了橡胶雷神枪攻击了%s", "%s使用了流樱霸气缠绕攻击了%s", "%s使用了霸王色施压攻击了%s"};
//凯多的技能
String[] attacks_kaiduo = {"%s使用了雷鸣八卦击打了%s", "%s使用了热息攻击了%s", "%s使用了轮回天冲攻击了%s", "%s使用了罡气攻击了%s"};
String[] injured = {"%s接住了%s的攻击并毫发无损", "%勉强的抗住了%s的伤害", "%s被%s的伤害打得泄气吐血", "%s被%s重伤"};
public Role() {
}
public Role(String name, int blood) {
this.name = name;
this.blood = blood;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public String[] getAttacks_lufei() {
return attacks_lufei;
}
public void setAttacks_lufei(String[] attacks_lufei) {
this.attacks_lufei = attacks_lufei;
}
public String[] getAttacks_kaiduo() {
return attacks_kaiduo;
}
public void setAttacks_kaiduo(String[] attacks_kaiduo) {
this.attacks_kaiduo = attacks_kaiduo;
}
public String[] getInjured() {
return injured;
}
public void setInjured(String[] injured) {
this.injured = injured;
}
//路飞的伤害技能
public void attack1(Role role) {
Random r = new Random();
int index = r.nextInt(attacks_lufei.length);
String ki = attacks_lufei[index];
//攻击效果
System.out.printf(ki, this.getName(), role.getName());
System.out.println(" ");
//定义造成伤害的值
int hurt = r.nextInt(99) + 1;
//修改被攻击者的血量
int remainBlood = role.getBlood() - hurt;
remainBlood = remainBlood < 0 ? 0 : remainBlood;
role.setBlood(remainBlood);
//受伤的描述
if (remainBlood > (remainBlood * 0.9)) {
System.out.printf(injured[0], role.getName(), this.getName());
System.out.println(" ");
} else if (remainBlood > (remainBlood * 0.7) && remainBlood > (remainBlood * 0.9)) {
System.out.printf(injured[1], role.getName(), this.getName());
System.out.println(" ");
} else if (remainBlood > (remainBlood * 0.4) && remainBlood > (remainBlood * 0.7) && remainBlood > (remainBlood * 0.9)) {
System.out.printf(injured[2], role.getName(), this.getName());
System.out.println(" ");
} else {
System.out.printf(injured[3], role.getName(), this.getName());
System.out.println(" ");
}
System.out.println("血量值还剩下" + getBlood());
}
//凯多的伤害技能
public void attack2(Role role) {
Random r = new Random();
int index = r.nextInt(attacks_lufei.length);
String ki = attacks_kaiduo[index];
//攻击效果
System.out.printf(ki, this.getName(), role.getName());
System.out.println(" ");
//定义造成伤害的值
int hurt = r.nextInt(99) + 1;
//修改被攻击者的血量
int remainBlood = role.getBlood() - hurt;
remainBlood = remainBlood < 0 ? 0 : remainBlood;
role.setBlood(remainBlood);
//受伤的描述
if (remainBlood > (remainBlood * 0.9)) {
System.out.printf(injured[0], role.getName(), this.getName());
System.out.println(" ");
} else if (remainBlood > (remainBlood * 0.7) && remainBlood > (remainBlood * 0.9)) {
System.out.printf(injured[1], role.getName(), this.getName());
System.out.println(" ");
} else if (remainBlood > (remainBlood * 0.4) && remainBlood > (remainBlood * 0.7) && remainBlood > (remainBlood * 0.9)) {
System.out.printf(injured[2], role.getName(), this.getName());
System.out.println(" ");
} else {
System.out.printf(injured[3], role.getName(), this.getName());
System.out.println(" ");
}
System.out.println("血量值还剩下" + getBlood());
}
}
测试代码
public class Game_test {
public static void main(String[] args) {
//第一个角色
Role j1 = new Role("尼卡觉醒*路飞",4000);
//第二个角色
Role j2 = new Role("龙形态*凯多",4000);
//回合制开始战斗
while (true){
//路飞攻击凯多
j1.attack1(j2);
//凯多剩余血量
if(j2.getBlood() == 0){
System.out.println(j2.getName()+"被击败");
break;
}
//凯多攻击路飞
j2.attack2(j1);
if(j1.getBlood() == 0){
System.out.println(j1.getName()+"被击败");
break;
}
}
}
}
执行效果
本练习较为简单,也有许多不足之处,由于制作时间较短 许多地方后续将会改进。