1、进行面向对象的案例
package com.myjava.playgame;
import java.util.Random;
public class user {
//姓名。血量,性别,长相
private String name;
private int blool;
private String 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一声惨叫,像滩软泥般塌了下去!"
};
//无参构造
user() {
}
//有参构造
user(String name, int blool, String gender) {
this.name = name;
this.blool = blool;
this.gender = gender;
setFace(gender);
}
//得到性别
public String getGender() {
return gender;
}
//设置性别
public void setGender(String gender) {
this.gender = gender;
}
//得到长相
public String getFace() {
return face;
}
//设置长相
public void setFace(String gender) {
Random r = new Random();
if (gender == "男") {
int index = r.nextInt(boyfaces.length);
this.face = boyfaces[index];
} else if (gender == "女") {
int index = r.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 getBlool() {
return blool;
}
//设置血量
public void setBlool(int blool) {
this.blool = blool;
}
//输出角色信息
public void outInfo() {
System.out.println("姓名 "+getName());
System.out.println("血量 "+getBlool());
System.out.println("性别 "+getGender());
System.out.println("长相 "+getFace());
}
//方法,打架过程
//r1打了r2
public void attack(user role) {
//输出招式
Random r = new Random();
int index = r.nextInt(attacks_desc.length);
String kungFu = attacks_desc[index];
System.out.printf(kungFu, this.getName(), role.getName());
//调用者造成的伤害
int hurt = r.nextInt(20) + 1;
System.out.println("造成了" + hurt + "伤害");
//被击打者剩下的血量
int keepBlool = role.getBlool() - hurt;
//判断剩下的血量是否为负a
if (keepBlool < 0) {
keepBlool = 0;
}
//重新赋值给被击打者设置血量
role.setBlool(keepBlool);
if (keepBlool > 90) {
System.out.printf(injureds_desc[0], role.getName());
} else if (keepBlool > 80) {
System.out.printf(injureds_desc[1], role.getName());
} else if (keepBlool > 70) {
System.out.printf(injureds_desc[2], role.getName());
} else if (keepBlool > 60) {
System.out.printf(injureds_desc[3], role.getName());
} else if (keepBlool > 40) {
System.out.printf(injureds_desc[4], role.getName());
} else if (keepBlool > 20) {
System.out.printf(injureds_desc[5], role.getName());
} else if (keepBlool > 10) {
System.out.printf(injureds_desc[6], role.getName());
} else {
System.out.printf(injureds_desc[7], role.getName());
}
//输出被击打着剩余的血量
System.out.println(role.getName() + "剩余" + keepBlool + "血量");
}
}
package com.myjava.playgame;
public class test {
public static void main(String[] args) {
//创建对象
user r1 = new user("角色1", 100, "男");
user r2 = new user("角色2", 100, "男");
//输出对象信息
r1.outInfo();
System.out.println("-----------------");
r2.outInfo();
System.out.println("-----------------");
//调用击打方法
while (true) {
r1.attack(r2);
if (r2.getBlool() == 0) {
System.out.println(r1.getName() + " KO了 " + r2.getName());
break;
}
r2.attack(r1);
if (r1.getBlool() == 0) {
System.out.println(r2.getName() + " KO了 " + r1.getName());
break;
}
}
}
}
2、String字符串与常用API
3、下周开始进阶内容