练习
package exercise3;public class FightTest {public static void main(String[] args) {Role r1 = new Role("kobe", 100);Role r2 = new Role("james", 100);while (true) {r1.attack(r2);if (isWin(r1,r2)) break;r2.attack(r1);if (isWin(r2,r1)) break;}}public static boolean isWin(Role r1, Role r2){if (r2.getBlood() == 0) {System.out.println(r1.getName() + "打败了" + r2.getName());return true;}else return false;}
}package exercise3;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;}/*** 获取** @return name*/public String getName() {return name;}/*** 设置** @param name*/public void setName(String name) {this.name = name;}/*** 获取** @return blood*/public int getBlood() {return blood;}/*** 设置** @param blood*/public void setBlood(int blood) {this.blood = blood;}public void attack(Role name) {Random r = new Random();int hurt = r.nextInt(20) + 1;int remainBlood = name.getBlood() - hurt;remainBlood = remainBlood < 0 ? 0 : remainBlood;name.setBlood(remainBlood);System.out.println(this.name + "给" + name.getName() + "一拳,造成了" + hurt + "伤害,"+ name.getName() + "还剩" + name.getBlood() + "点血。");}
}
%s 的作用
Alt+insert :get/set成员变量快捷键
格斗游戏改进
package exercise3;public class FightTest {public static void main(String[] args) {Role r1 = new Role("kobe", 100, '男');Role r2 = new Role("james", 100, '女');r1.showRoleInfo();r2.showRoleInfo();while (true) {r1.attack(r2);if (isWin(r1, r2)) break;r2.attack(r1);if (isWin(r2, r1)) break;}}public static boolean isWin(Role r1, Role r2) {if (r2.getBlood() == 0) {System.out.println(r1.getName() + "打败了" + r2.getName());return true;} else return false;}
}package exercise3;import java.util.Random;public class Role {private String name;private int blood;private char gender;private String face;String[] boyfaces = {"风流俊雅", "气宇轩昂", "相貌英俊", "五官端正", "相貌平平", "一塌糊涂", "面目狰狞"};String[] girlfaces = {"美奂绝伦", "沉鱼落雁", "婷婷玉立", "身材娇好", "相貌平平", "相貌简陋", "惨不忍睹"};//attack 攻击描述:String[] attacks_desc = {"%s使出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去。","%s使出了一招【游空探爪】,飞起身形自半空中变掌为抓锁向%s。","%s大喝一声,身形下伏,一招【劈雷坠地】,捶向%s双腿。","%s运气于掌,一瞬间掌心变得血红,一式【掌心雷】,推向%s。","%s阴手翻起阳手跟进,一招【没遮拦】,结结实实的捶向%s。","%s上步抢身,招中套招,一招【劈挂连环】,连环攻向%s。"};//injured 受伤描述:String[] injureds_desc = {"结果%s退了半步,毫发无损","结果给%s造成一处瘀伤","结果一击命中,%s痛得弯下腰","结果%s痛苦地闷哼了一声,显然受了点内伤","结果%s摇摇晃晃,一跤摔倒在地","结果%s脸色一下变得惨白,连退了好几步","结果『轰』的一声,%s口中鲜血狂喷而出","结果%s一声惨叫,像滩软泥般塌了下去"};public Role() {}public Role(String name, int blood) {this.name = name;this.blood = blood;}public Role(String name, int blood, char gender) {this.name = name;this.blood = blood;this.gender = gender;setFace(gender);this.face = getFace();}/*** 获取** @return name*/public String getName() {return name;}/*** 设置** @param name*/public void setName(String name) {this.name = name;}/*** 获取** @return blood*/public int getBlood() {return blood;}/*** 设置** @param blood*/public void setBlood(int blood) {this.blood = blood;}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(boyfaces.length);this.face = boyfaces[index];} else if (gender == '女') {int index = r.nextInt(girlfaces.length);this.face = girlfaces[index];} else {this.face = "惨不忍睹";}}public void showRoleInfo() {System.out.println("姓名:" + getName());System.out.println("血量:" + getBlood());System.out.println("性别:" + getGender());System.out.println("面容:" + getFace());}public void attack(Role role) {Random r = new Random();int hurt = r.nextInt(20) + 1;int remainBlood = role.getBlood() - hurt;remainBlood = remainBlood < 0 ? 0 : remainBlood;role.setBlood(remainBlood);int index = r.nextInt(attacks_desc.length);System.out.printf(attacks_desc[index], this.name, role.getName());System.out.println();if (remainBlood >= 90) {System.out.printf(injureds_desc[0], role.getName());} else if (remainBlood >= 80 && remainBlood < 90) {System.out.printf(injureds_desc[1], role.getName());} else if (remainBlood >= 70 && remainBlood < 80) {System.out.printf(injureds_desc[2], role.getName());} else if (remainBlood >= 60 && remainBlood < 70) {System.out.printf(injureds_desc[3], role.getName());} else if (remainBlood >= 40 && remainBlood < 60) {System.out.printf(injureds_desc[4], role.getName());} else if (remainBlood >= 20 && remainBlood < 40) {System.out.printf(injureds_desc[5], role.getName());} else if (remainBlood >= 10 && remainBlood < 20) {System.out.printf(injureds_desc[6], role.getName());} else {System.out.printf(injureds_desc[7], role.getName());}System.out.println();}}
键盘录入 制表符%t
记得先写出一个标准的Javabean类创建对象(包括属性,空参构造方法,带参构造方法,set/get 成员方法)
注意:数组中如果存在null,则null是不能使用的,如果创建一个对象是null,然后调用这个对象方法,代码就会报错。
package exercise5;import java.util.Scanner;public class StudentTest {public static void main(String[] args) {Student[] studentArr = new Student[3];Student s1 = new Student(1, "xiaoa", 20);Student s2 = new Student(2, "xiaob", 21);Student s3 = new Student(4, "xiaoc", 22);studentArr[0] = s1;studentArr[1] = s2;studentArr[2] = s3;Student s4 = new Student(3, "xiaod", 23);if (contains(studentArr, s4.getId())) {System.out.println("学号已存在");} else {//判断数组存满没有?int count = getCount(studentArr);if (count == studentArr.length) {//存满了studentArr = addStudent(s4, studentArr);showArr(studentArr);} else {//没满studentArr[count] = s4;showArr(studentArr);}}studentArr = delStudent(studentArr);showArr(studentArr);studentArr = findId(studentArr);showArr(studentArr);}public static boolean contains(Student[] studentArr, int newStudentId) {for (Student student : studentArr) {if (student != null && student.getId() == newStudentId) {return true;}}return false;}public static int getCount(Student[] studentArr) {int count = 0;for (Student student : studentArr) {if (student != null) {count++;}}return count;}public static Student[] addStudent(Student newStudent, Student[] studentArr) {Student[] newStudentArr = new Student[studentArr.length + 1];for (int i = 0; i < studentArr.length; i++) {newStudentArr[i] = studentArr[i];}newStudentArr[studentArr.length] = newStudent;return newStudentArr;}public static void showArr(Student[] arr) {for (int i = 0; i < arr.length; i++) {if (arr[i] != null) {System.out.println(arr[i].getId() + ", " + arr[i].getName() + ", " + arr[i].getAge());}}}public static Student[] delStudent(Student[] arr) {Scanner sc = new Scanner(System.in);System.out.println("请输入删除学生的学号:");int delStuId = sc.nextInt();int index = getIndex(arr, delStuId);if (index >= 0) {//存在该学生arr[index] = null;System.out.println("存在该学生");} else {//不存在System.out.println("不存在该学生,删除失败");}return arr;}public static int getIndex(Student[] arr, int delStuId) {for (int i = 0; i < arr.length; i++) {Student stu = arr[i];if (stu != null && stu.getId() == delStuId) {return i;}}return -1;}public static Student[] findId(Student[] arr) {Scanner sc = new Scanner(System.in);System.out.println("输入要查找的学生id:");int findId = sc.nextInt();int index = getIndex(arr, findId);if (index >= 0) {arr[index].setAge(arr[index].getAge() + 1);} else {System.out.println("不存在这个id");}return arr;}
}package exercise5;public class Student {private int id;private String name;private int age;public Student() {}public Student(int id, String name, int age) {this.id = id;this.name = name;this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public boolean isExist(Student[] arr, int newId) {for (int i = 0; i < arr.length; i++) {if (arr[0].getId() == newId) {return true;}}return false;}}