题目来源:
https://www.bilibili.com/video/BV17F411T7Ao?p=91&vd_source=45bbb703a417550ebf434e539410a703https://www.bilibili.com/video/BV17F411T7Ao?p=91&vd_source=45bbb703a417550ebf434e539410a703 最终效果实现
Javabean类:
私有属性,数组参数
public class Role {
private String name;//姓名
private int blood;//血量
private char gender;//性别
private String face;//长相
//男角色长相数组
String[] boyfaces={"风流俊雅","器宇轩昂","相貌英俊","五官端正","风神俊朗"};
//女角色长相数组
String[] girlfaces={"沉鱼落雁","亭亭玉立","身材姣好","美奂绝伦","相貌平平"};
//攻击招式数组
String[] attacks_desc={"%s使出了一招【大悲赋第一式:天绝地灭大搜魂手】,转到对方的身后,一掌向%s背心的灵台穴拍去。",
"%s使出了一招【沧海六合】,飞起身形自半空中变掌为抓锁向%s。",
"%s大喝一声,身形下伏,一招【大悲赋第二式:天阴地阳大紫炎气】,捶向%s双腿。",
"%s运气于掌,一瞬间掌心变得血红,一式【大悲赋第三式:诛神杀圣弑仙指】,推向%s。",
"%s阴手翻起阳手跟进,一招【红尘九天破剑道】,结结实实的捶向%s。",
"%s上步抢身,招中套招,一招【独孤九剑:破剑式】,连环攻向%s。"};
//人物状态数组
String[] injureds_desc={"结果%s退了半步,毫发无损",
"结果给%s造成一处瘀伤",
"结果一击命中,%s痛得弯下腰",
"结果%s痛苦地闷哼了一声,显然受了点内伤",
"结果%s摇摇晃晃,一跤摔倒在地",
"结果%s脸色一下变得惨白,连退了好几步",
"结果『轰』的一声,%s口中鲜血狂喷而出",
"结果%s一声惨叫,像滩软泥般塌了下去"};
构造方法,Getter/setter方法
public Role(String name, int blood,char gender) {
this.name = name;
this.blood = blood;
this.gender=gender;
//随机长相,把性别传入setFace中进行判断
setFace(gender);
}
public Role() {
this.name = name;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getFace() {
return face;
}
public void setFace(char gender) {
this.face = face;
//判断性别
if (gender=='男'){
//从数组中随机下标
int index = new Random().nextInt(boyfaces.length);
//根据下标随机数组的值,达到随机长相的目的
this.face = boyfaces[index];
}else if (gender=='女'){
int index = new Random().nextInt(girlfaces.length);
this.face=girlfaces[index];
}else{
this.face="相貌平平";
}
}
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 void fight(Role role){
//随机招式
int index = new Random().nextInt(attacks_desc.length);
String kongfu = attacks_desc[index];
//输出攻击的效果
System.out.printf(kongfu,this.getName(),role.getName());
//换行
System.out.println();
//定义随机伤害
Random r = new Random();
int hurt = r.nextInt(20)+1;
//剩余血量
int remainBoold = role.getBlood()-hurt;
//如果剩余血量<0则重置为0
if (remainBoold<0){
remainBoold=0;
}
//角色的血量设置为剩余血量
role.setBlood(remainBoold);
if (remainBoold>90){
System.out.printf(injureds_desc[0],role.getName());
}else if (remainBoold>80){
System.out.printf(injureds_desc[1],role.getName());
}else if (remainBoold>70){
System.out.printf(injureds_desc[2],role.getName());
}else if (remainBoold>60){
System.out.printf(injureds_desc[3],role.getName());
}else if (remainBoold>40){
System.out.printf(injureds_desc[4],role.getName());
}else if (remainBoold>20){
System.out.printf(injureds_desc[5],role.getName());
}else if (remainBoold>10){
System.out.printf(injureds_desc[6],role.getName());
}else{
System.out.printf(injureds_desc[7],role.getName());
}
System.out.println();
// System.out.println(this.getName()+"举起拳头打了"+role.getName()+"一下,造成了"+hurt+"点伤害,"+role.getName()+"还剩下"+role.getBlood()+"血" +
// "");
}
//输出人物的信息
public void showRoleInfo(){
System.out.println("姓名为"+getName());
System.out.println("性别为"+getGender());
System.out.println("长相为"+getFace());
System.out.println("血量为"+getBlood());
}
}
测试类:
public class MainApp {
public static void main(String[] args) {
//对象实例化
Role r1 = new Role("沈孤鸿",100,'男');
Role r2 = new Role("沈孤雁",100,'男');
//调用对象的信息属性,输出信息
r1.showRoleInfo();
r2.showRoleInfo();
System.out.println("世间最强的武器,是大悲赋和孔雀翎。世上最悲哀的对决,是赴死之时,早已失去一切");
System.out.println("------鸿雁之决-----");
//循环次数不定使用while(true)
while(true){
r1.fight(r2);
if (r2.getBlood()==0){
System.out.println(r1.getName()+"ko了"+r2.getName());
break;
}
r2.fight(r1);
if (r1.getBlood()==0){
System.out.println(r2.getName()+"ko了"+r1.getName());
break;
}
}
}
}
注:
1.idea自动生成构造/getter and setter方法:
2.
System.out.printf
第一部分参数:输出的的内容%s(占位)
第二部分参数:填充的数据
例: