1.文字版格斗游戏
package duixiang01; 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[] attack_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, char gender) { this.name = name; this.blood = blood; this.gender = gender; //随机长相 setFace(gender); } public char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } public String getFace() { return face; } public void setFace(char face) { 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 = "面目狰狞"; } } /** * 获取 * * @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; } //定义一个方法攻击别人 //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 KungFu = attack_desc[index]; //输出一个攻击效果 System.out.printf(KungFu, this.getName(), role.getName()); System.out.println(); //造成的伤害1~20 int hurt = r.nextInt(20) + 1; //剩余血量 int remainBoold = role.getBlood() - hurt; //对剩余血量进行一个验证 remainBoold = remainBoold < 0 ? 0 : remainBoold; //修改一下挨揍人的血量 role.setBlood(remainBoold); //受伤的描述 //血量>90 0索引的描述 //80~90 1索引的描述 //70~80 2索引的描述 //60~70 3索引的描述 //40~60 4索引的描述 //20~40 5索引的描述 //10~20 6索引的描述 //小于10的 7索引的描述 if (remainBoold > 90) { System.out.printf(injureds_desc[0], role.getName()); } else if (remainBoold > 80) { System.out.printf(injureds_desc[1], role.getName()); } else if (remainBoold > 70) { System.out.printf(injureds_desc[2], role.getName()); } else if (remainBoold > 60) { System.out.printf(injureds_desc[3], role.getName()); } else if (remainBoold > 40) { System.out.printf(injureds_desc[4], role.getName()); } else if (remainBoold > 20) { System.out.printf(injureds_desc[5], role.getName()); } else if (remainBoold > 10) { System.out.printf(injureds_desc[6], role.getName()); } else { System.out.printf(injureds_desc[7], role.getName()); ; } System.out.println(); } public void showRoleInfo() { System.out.println("姓名为:" + getName()); System.out.println("性别为:" + getGender()); System.out.println("血量为:" + getBlood()); System.out.println("长相为:" + getFace()); } }
package duixiang01; public class GameTest { public static void main(String[] args) { //1.创建第一个角色 Role r1 = new Role("乔峰", 100, '男'); //2.创建第二个角色 Role r2 = new Role("鸠摩智", 100, '男'); //展示一下角色信息 r1.showRoleInfo(); r2.showRoleInfo(); while (true) { //r1开始攻击r2 r1.attack(r2); if (r2.getBlood() == 0) { System.out.println(r1.getName() + "ko了," + r2.getName()); break; } //r2开始攻击r1 r2.attack(r1); if (r1.getBlood() == 0) { System.out.println(r2.getName() + "ko了," + r1.getName()); break; } } } }
2.对象数组练习
1)对象数组1
定义数组存储3个商品对象
商品的属性:商品id,名字,价格,库存
创建三个商品对象,并把商品对象存入到数组当中
package duixiang01; public class Goods { private String id; private String name; private double price; private int count; public Goods() { } public Goods(String id, String name, double price, int count) { this.id = id; this.name = name; this.price = price; this.count = count; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
package duixiang01; public class GoodsTest { public static void main(String[] args) { Goods[] arr = new Goods[3]; Goods g1 = new Goods("001", "华为P40", 5999.0, 499); Goods g2 = new Goods("002", "小米14", 3666, 49); Goods g3 = new Goods("003", "保温杯", 59, 99); arr[0] = g1; arr[1] = g2; arr[2] = g3; for (int i = 0; i < arr.length; i++) { Goods goods = arr[i]; System.out.println(goods.getId() + "," + goods.getName() + "," + goods.getPrice() + "," + goods.getCount()); } } }
2)对象数组2
定义数组存储3部汽车对象
汽车的属性:品牌,价格,颜色
创建三个汽车对象,数据通过键盘录入而来,并把数据存入到数组当中
package duixiang01; public class Car { private String brand; private int price; private String color; public Car() { } public Car(String brand, int price, String color) { this.brand = brand; this.price = price; this.color = color; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
package duixiang01; import java.util.Scanner; public class CarTest { public static void main(String[] args) { Car[] arr = new Car[3]; Scanner sc = new Scanner(System.in); for (int i = 0; i < arr.length; i++) { Car c = new Car(); System.out.println("请输入汽车品牌"); String brand = sc.next(); c.setBrand(brand); System.out.println("请输入汽车价格"); int price = sc.nextInt(); c.setPrice(price); System.out.println("请输入汽车颜色"); String color = sc.next(); c.setColor(color); arr[i] = c; } for (int i = 0; i < arr.length; i++) { Car car = arr[i]; System.out.println(car.getBrand() + "," + car.getPrice() + "," + car.getColor()); } } }
3)对象数组3
定义数组存储3部手机对象
手机的属性:品牌,价格,颜色
要求,计算出三部手机的平均价格
package duixiang01; public class Phone { private String brand; private int price; private String color; public Phone() { } public Phone(String brand, int price, String color) { this.brand = brand; this.price = price; this.color = color; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
package duixiang01; public class PhoneTest { public static void main(String[] args) { Phone[] arr = new Phone[3]; Phone p1 = new Phone("小米", 5999, "红色"); Phone p2 = new Phone("华为", 2999, "黑色"); Phone p3 = new Phone("荣耀", 3999, "绿色"); arr[0] = p1; arr[1] = p2; arr[2] = p3; int sum = 0; for (int i = 0; i < arr.length; i++) { Phone phone = arr[i]; sum = sum + phone.getPrice(); } int avg = sum / arr.length; System.out.println(avg); } }
4)对象数组4
定义数组存储4个女朋友的对象
女朋友的属性:姓名、年龄、性别、爱好
要求1:计算出4个女朋友的平均年龄
要求2:统计年龄比平均值低的女朋友有几个?并把她们的所以信息打印出来
package duixiang01; public class GirlFriend { private String name; private int age; private String gender; private String hobby; public GirlFriend() { } public GirlFriend(String name, int age, String gender, String hobby) { this.name = name; this.age = age; this.gender = gender; this.hobby = hobby; } 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 String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } }
package duixiang01; public class GirlFriendTest { public static void main(String[] args) { GirlFriend[] arr = new GirlFriend[4]; GirlFriend g1 = new GirlFriend("小美", 18, "萌妹子", "唱歌"); GirlFriend g2 = new GirlFriend("小刘", 19, "憨妹子", "唱歌"); GirlFriend g3 = new GirlFriend("小杨", 28, "萌妹子", "唱歌"); GirlFriend g4 = new GirlFriend("小张", 22, "萌妹子", "唱歌"); arr[0] = g1; arr[1] = g2; arr[2] = g3; arr[3] = g4; int sum = 0; for (int i = 0; i < arr.length; i++) { GirlFriend girlFriend = arr[i]; sum = sum + girlFriend.getAge(); } int avg = sum / arr.length; System.out.println("平均年龄:" + avg); int count = 0; for (int i = 0; i < arr.length; i++) { GirlFriend girlFriend = arr[i]; if (girlFriend.getAge() < avg) { count++; System.out.println(girlFriend.getName() + "," + girlFriend.getAge() + "," + girlFriend.getGender() + "," + girlFriend.getHobby()); } } System.out.println(count + "个"); } }
5)对象数组5
定义一个数组长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同。
学生的属性:学号,姓名,年龄
要求3:通过id删除学生信息
如果存在,则删除,如果不存在,则提示删除失败
要求4:删除完毕之后,遍历所以学生信息
package duixiang01; public class Test1 { public static void main(String[] args) { Student[] arr = new Student[3]; Student stu1 = new Student(1, "张三", 20); Student stu2 = new Student(2, "李四", 23); Student stu3 = new Student(3, "王五", 28); arr[0] = stu1; arr[1] = stu2; arr[2] = stu3; //找到id在数组中对应索引 int index = getIndex(arr, 3); if (index >= 0) { arr[index] = null; System.out.println("删除成功"); printArr(arr); } else { System.out.println("删除失败"); } } public static void printArr(Student[] arr) { for (int i = 0; i < arr.length; i++) { Student stu = arr[i]; if (stu != null) { System.out.println(stu.getId() + "," + stu.getId() + "," + stu.getName() + "," + stu.getAge()); } } } public static int getIndex(Student[] arr, int id) { for (int i = 0; i < arr.length; i++) { Student stu = arr[i]; if (arr[i] != null) { if (stu.getId() == id) { return i; } } } return -1; } }
6)对象数组6
要求5查询数组id为"2"的学生,如果存在,则将他的年龄加1岁
package duixiang01; public class Test2 { public static void main(String[] args) { Student[] arr = new Student[3]; Student stu1 = new Student(1, "张三", 20); Student stu2 = new Student(2, "李四", 23); Student stu3 = new Student(3, "王五", 28); arr[0] = stu1; arr[1] = stu2; arr[2] = stu3; int index = getIndex(arr, 4); if (index >= 0) { Student stu = arr[index]; stu.setAge(stu.getAge() + 1); printArr(arr); } else { System.out.println("该id学生不存在,修改失败"); } } public static int getIndex(Student[] arr, int id) { for (int i = 0; i < arr.length; i++) { Student stu = arr[i]; int sid = stu.getId(); if (sid == id) { return i; } } return -1; } public static void printArr(Student[] arr) { for (int i = 0; i < arr.length; i++) { Student stu = arr[i]; if (stu != null) { System.out.println(stu.getId() + "," + stu.getId() + "," + stu.getName() + "," + stu.getAge()); } } } }