91 javabean 格式 乔峰和鸠摩智回合制格斗
对象-设计图
package com.fqs.combat; import java.util.Random; 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; } //格斗 需要 ko // Role r1=new Role(); // Role r2=new Role(); //r1.playKo(r2) public void playKo(Role role){ //之前的血量是blood //随机打击是 strike Random r=new Random(); int strike=r.nextInt(20); //剩余血量 role.blood= role.blood-strike; System.out.println(this.name +"打了"+role.name+"一拳,"+role.name+"受到了"+strike+"个伤害,"+role.name+"剩余"+role.blood+"个血量"); } }
方法调用
package com.fqs.combat; public class Ko { public static void main(String[] args) { Role r1=new Role("乔峰",100); Role r2=new Role("鸠摩智",100); while(true){ if(r1.getBlood()>0&&r2.getBlood()>0){ r1.playKo(r2); r2.playKo(r1); if(r1.getBlood()<0||r2.getBlood()<0){ if(r2.getBlood()<0){ System.out.println("乔峰赢"); }else{ System.out.println("鸠摩智赢"); } } } } } }
第二步
传递参数 拼接 招式
对象
package com.fqs.javaBean; import java.util.Random; public class Role { private String name; private int blood; private char sex; Random r=new Random(); //受伤描述 String[] shangHai_desc={ "结果%s退后半步,毫发无损", "结果一击命中,%s痛得晕死过去,再也没有还手之力" }; public Role() { } public Role(String name, int blood,char sex) { this.name = name; this.blood = blood; this.sex=sex; } 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 char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public void getFace(Role role) { //长相的描述 String[] face_nan_desc={"天仙下凡","平平无奇","面目狰狞"}; String[] face_nv_desc={"风流俊雅","绿茶清新","面目狰狞"}; System.out.println("role.sex:"+role.sex); System.out.println("role.name:"+role.name); System.out.println("role.blood:"+role.blood); if(role.sex=='男'){ String face=face_nan_desc[r.nextInt(face_nan_desc.length)]; System.out.println(role.name+"男相貌:"+face); } else if(role.sex=='女'){ String face=face_nv_desc[r.nextInt(face_nv_desc.length)]; System.out.println(role.name+"女相貌:"+face); }else{ String face="面目狰狞"; System.out.println(role.name+"无性别相貌:"+face); } } public void get_ko_desc(Role role) { //攻击的描述 String[] ko_desc={ "%s使出了一招【背心钉】,转到对方身后,一掌向%s背后的灵台穴拍去", "%s上步抢身,招中套招,一招【劈挂连环】,连环攻向%s" }; String ko=ko_desc[r.nextInt(ko_desc.length)]; System.out.printf(ko,this.name,role.name); System.out.println(" "); } public void playKo(Role role) { //对象具体化 //Role r1=new Role //Role r2=new Role //r1打r2 r1.playKo(r2) Random r=new Random(); int shangHai=r.nextInt(20); role.blood= role.blood-shangHai; System.out.println(this.name+"打了"+role.name+","+role.name+"受到了"+shangHai+"个伤害,"+role.name+"剩余"+role.blood+"个血量"); } }
调用方法
package com.fqs.javaBean; public class PlayRole { public static void main(String[] args) { Role r1 = new Role("乔峰", 100,'男'); Role r2 = new Role("鸠摩智", 100,'男'); r1.getFace(r1); r2.getFace(r2); r1.get_ko_desc(r2); r2.get_ko_desc(r1); // while (r1.getBlood() > 0 && r2.getBlood() > 0) { // r1.playKo(r2); // r2.playKo(r1); // if (r1.getBlood() < 0 || r2.getBlood() < 0) { // if (r1.getBlood() < 0) { // System.out.println("乔峰输了"); // } else { // System.out.println("鸠摩智输了"); // } // } // }//结束while循环 } }