public class Role {private String name;private int blood;//空参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;}//定义一个方法用于攻击别人//思考,谁攻击谁?//Role r1=new Role();//Role r2=new Role();//r1.攻击(r2)//方法的调用者去攻击参数public void attack(Role role){//计算造成的伤害1~20Random r=new Random();int hurt=r.nextInt(20)+1;//剩余血量int remainBoold=role.getBlood()-hurt;//对剩余血量做一个验证,如果为负数了,就修改为0remainBoold=remainBoold<0?0:remainBoold;//修改一下被揍的人的血量role.setBlood(remainBoold);//this表示方法的调用者System.out.println(this.getName()+"举起了拳头,打了"+role.getName()+"一下,"+"造成了"+hurt+"点伤害,这个人还剩下了"+remainBoold+"点血量" );} }
public class GameTest {public static void main(String[] args) {//创建第一个角色Role r1 = new Role("乔峰",100);//创建第二个角色Role r2=new Role("张三丰",100);//3.开始格斗, 回合制游戏while (true){//r1攻击r2r1.attack(r2);//判断r2的剩余血量if(r2.getBlood()==0){System.out.println(r1.getName()+"K.O了"+r2.getName());break;}//r2r攻击r1r2.attack(r1);if(r1.getBlood()==0){System.out.println(r2.getName()+"K.O了"+r1.getName());break;}}} }
以下是优化方式:
import java.util.Random;public class Role {private String name;private int blood;private char gender;private String face;//长相是随机的String[]boyfaces={"风流俊雅","气宇轩昂","相貌英俊","五官端正","相貌平平","一塌糊涂","面目狰狞"};String[]girlfaces={"美艳绝伦","沉鱼落雁","亭亭玉立","身材姣好","相貌平平","丑的不行","惨不忍睹"};String[]attack_desc={"%s使出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去","%s使出了一招【游空探爪】,飞起身形自半空中边变掌为抓锁向%s","%s大喝一声,身形下伏,一招【霹雷坠地】,垂向%s双腿","%s运气于掌,一瞬间掌心变得血红,一式掌心雷,推向%s",};String[]injured_desc={"结果%s退了半步,毫发无损","结果给%s造成一处瘀伤","结果一击命中,%s痛苦的弯下了腰","%s痛苦的闷哼了一声,显然受了点内伤",};//包含全部参数的构造public Role(String name, int blood, char gender) {this.gender=gender;this.name = name;this.blood = blood;setFace(gender);}public String getFace() {return face;}public void setFace(char gender) {Random random=new Random();//长相是随机的if(gender=='男'){//从boyfaces里面随机长相//index只是一个索引,我们还需要把数据提取出来int index= random.nextInt(boyfaces.length);//我们只要把索引给这个数组,然后在赋值给this指定的变量就行了this.face=boyfaces[index];} else if (gender=='女') {//从girlfaces里面随机长相int index= random.nextInt(girlfaces.length);this.face=girlfaces[index];}else{this.face = "面目狰狞";}this.face = face;}public char getGender() {return gender;}public void setGender(char gender) {this.gender = gender;}//空参public Role() {}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;}//定义一个方法用于攻击别人//思考,谁攻击谁?//Role r1=new Role();//Role r2=new Role();//r1.攻击(r2)//方法的调用者去攻击参数public void attack(Role role){Random r=new Random();int index=r.nextInt(attack_desc.length);String KongFu=attack_desc[index];//输出一个攻击的效果System.out.printf(KongFu,this.getName(),role.getName());//计算造成的伤害1~20int hurt=r.nextInt(20)+1;//剩余血量int remainBoold=role.getBlood()-hurt;//对剩余血量做一个验证,如果为负数了,就修改为0remainBoold=remainBoold<0?0:remainBoold;//修改一下被揍的人的血量role.setBlood(remainBoold);//受伤的描述System.out.printf(KongFu,this.getName(),role.getName());//this表示方法的调用者System.out.println(this.getName()+"举起了拳头,打了"+role.getName()+"一下,"+"造成了"+hurt+"点伤害,这个人还剩下了"+remainBoold+"点血量" );}public void showRoleInfo(){System.out.println("姓名为"+getName());System.out.println("血量为"+getBlood());System.out.println("性别为"+getGender());System.out.println("长相为"+getFace());} }
public class GameTest {public static void main(String[] args) {//创建第一个角色Role r1 = new Role("乔峰",100,'男');//创建第二个角色Role r2=new Role("张三丰",100,'男' );//展示一下信息r1.showRoleInfo();r2.showRoleInfo();//3.开始格斗, 回合制游戏while (true){//r1攻击r2r1.attack(r2);//判断r2的剩余血量if(r2.getBlood()==0){System.out.println(r1.getName()+"K.O了"+r2.getName());break;}//r2r攻击r1r2.attack(r1);if(r1.getBlood()==0){System.out.println(r2.getName()+"K.O了"+r1.getName());break;}}} }