package pxc.cn;
import java.util.Random;
public class role {
private String name;
private int blood;
private char gender;
private String face;//脸是随机的
//男女随机数组
String[] boyface={"风流","轩昂","英俊","端正","平平","糊涂","狰狞"};
String[] girlface={"美丽","落雁","玉立","娇好","平平","简陋","惨痛"};
//攻击描述
String[] gj={"%s使出了背心,转到%s身后拍去",
"%s使出了大米,转到身后打去%s",
"%s使出了啊啊,转到身后%s双腿",
"%s使出了背心,转到身后推向%s",
"%s使出了无敌,转到身后吹向%s"};
//受伤描述
String[] ss={
"结果%s退了半步 没事",
"结果给%s造成小伤",
"结果击中,%s痛弯下腰",
"结果%s痛苦哼,受内伤",
"结果倒地不起"
};
public role(){
}
public role(String name,int blood,char gender){
this.blood=blood;
this.name=name;
this.gender=gender;
//调用随机长相
setFace(gender);
}
public char getGender(){
return gender;
}
public void setGender(char gender){
this.gender=gender;
}
public String getFace(){
return face;
}
public void setFace(char gender){
Random r=new Random();
//男女长相随机
if (gender=='男'){
int index=r.nextInt(boyface.length);
this.face=boyface[index];
}else if (gender=='女'){
int index=r.nextInt(girlface.length);
this.face=girlface[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;
}
//定义攻击的方法
// 方法的调用者this 去攻击参数
public void attack(role Role){
Random r=new Random();
//攻击的随机
int index=r.nextInt(gj.length);
String kfu=gj[index];
//输出一个攻击效果 gj这个数组中有俩个%s 然后呢第一个%s 打人的this 被打的Role 输出souf
System.out.printf(kfu,this.getName(),Role.getName());
System.out.println();
//随机造成伤害1到20
int hurt=r.nextInt(20)+1;
//剩余血量
int remainblood=Role.getBlood()-hurt;
//看看剩余血量<0不 就是负数
remainblood=remainblood<0?0:remainblood;
//修改挨打人血量
Role.setBlood(remainblood);
//受伤描述多少血量
if (remainblood>80){
System.out.printf(ss[0],Role.getName());
}else if (remainblood>60){
System.out.printf(ss[1],Role.getName());
}else if (remainblood>20){
System.out.printf(ss[2],Role.getName());
}else {
System.out.printf(ss[3],Role.getName());
}
}
//角色信息
public void showinfo(){
System.out.println("姓名为"+getName());
System.out.println("性别为"+getGender());
System.out.println("血量为"+getBlood());
System.out.println("颜值为"+getFace());
System.out.println();
}
}
package pxc.cn;
public class name {
public static void main(String[] args) {
//创建角色
role s1 = new role("亚索", 100,'男');
role s2 = new role("薇恩", 100,'女');
//个人信息
s1.showinfo();
s2.showinfo();
//回合制攻击
while (true) {
//s1攻击s2
s1.attack(s2);
//判断血量
if (s2.getBlood() == 0) {
System.out.println(s1.getName() + "ko了" + s2.getName());
break;
}
//s2攻击s1
s2.attack(s1);
if (s1.getBlood() == 0) {
System.out.println(s2.getName() + "ko了" + s1.getName());
break;
}
}
}
}