author:&Carlton
language:JAVA
website: b站黑马程序员
黑马程序员 JAVA 2022斯坦福大学合集面向对象程序设计练习题
目录
Role类
RoleTest类
package com.itheima.demo2;
import java.util.Random;
public class Role {
private String name;
private String face;
private String gender;
private int blood;
//容貌:
String[] boyface = {"风流俊雅", "气宇轩昂", "相貌英俊", "五官端正", "相貌平平", "一塌糊涂", "面目狰狞"};
String[] girlface = {"美奂绝伦", "沉鱼落雁", "婷婷玉立", "身材娇好", "相貌平平", "相貌简陋", "惨不忍睹"};
//攻击描述:
String[] attack_desc = {
"%s使出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去。",
"%s使出了一招【游空探爪】,飞起身形自半空中变掌为抓锁向%s。",
"%s大喝一声,身形下伏,一招【劈雷坠地】,捶向%s双腿。",
"%s运气于掌,一瞬间掌心变得血红,一式【掌心雷】,推向%s。",
"%s阴手翻起阳手跟进,一招【没遮拦】,结结实实的捶向%s。",
"%s上步抢身,招中套招,一招【劈挂连环】,连环攻向%s。"};
//受伤描述:
String[] injured_desc = {
"结果%s退了半步,毫发无损",
"结果给%s造成一处瘀伤",
"结果一击命中,%s痛得弯下腰",
"结果%s痛苦地闷哼了一声,显然受了点内伤",
"结果%s摇摇晃晃,一跤摔倒在地",
"结果%s脸色一下变得惨白,连退了好几步",
"结果『轰』的一声,%s口中鲜血狂喷而出",
"结果%s一声惨叫,像滩软泥般塌了下去"};
public Role(String name, String gender, int blood) {
this.name = name;
this.gender = gender;
this.blood = blood;
setFace();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFace() {
return face;
}
public void setFace() {
if (this.gender == "男") {
Random r = new Random();
int index = r.nextInt(boyface.length);
this.face = boyface[index];
} else if (this.gender == "女") {
Random r = new Random();
int index = r.nextInt(girlface.length);
this.face = girlface[index];
} else {
this.face = "面目狰狞,认不出性别";
}
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
//展示角色信息
public void showInfo() {
System.out.println("姓名:" + this.name);
System.out.println("性别:" + this.gender);
System.out.println("面貌:" + this.face);
System.out.println("血量:" + this.blood);
}
//本对象攻击另一个对象
public void attack(Role rival) {
//1.攻击描述
Random r = new Random();
int index = r.nextInt(attack_desc.length);
System.out.printf(attack_desc[index], this.getName(), rival.getName());
//2.计算血量
int hurt = r.nextInt(20);
rival.blood -= hurt;
//血量不会小于0
rival.blood = rival.blood < 0 ? 0 : rival.blood;
//3.受伤描述
if (rival.blood >= 90) {
System.out.printf(injured_desc[0], rival.getName());
} else if (rival.blood >= 80) {
System.out.printf(injured_desc[1], rival.getName());
} else if (rival.blood >= 60) {
System.out.printf(injured_desc[2], rival.getName());
} else if (rival.blood >= 50) {
System.out.printf(injured_desc[3], rival.getName());
} else if (rival.blood >= 40) {
System.out.printf(injured_desc[4], rival.getName());
} else if (rival.blood >= 30) {
System.out.printf(injured_desc[5], rival.getName());
} else if (rival.blood > 0) {
System.out.printf(injured_desc[6], rival.getName());
} else {
System.out.printf(injured_desc[7], rival.getName());
}
System.out.println();
//4.显示血量
System.out.printf("【系统提示】%s还剩%d滴血", rival.getName(), rival.blood);
System.out.println();
}
}
含主函数入口
package com.itheima.demo2;
public class RoleTest {
//模拟角色打斗过程
public static void main(String[] args) {
Role a = new Role("张三", "男", 100);
Role b = new Role("李四", "女", 100);
a.showInfo();
System.out.println();
b.showInfo();
while(true){
a.attack(b);
if(b.getBlood() == 0){
System.out.println(a.getName() + "KO了" + b.getName());
break;
}
b.attack(a);
if(a.getBlood() == 0){
System.out.println(b.getName() + "KO了" + a.getName());
break;
}
}
}
}
欢迎转载,分享与指正,谢谢!