当前位置: 首页 > news >正文

《面向对象综合训练01~05》

《面向对象综合训练01~05》

训练01:文字版格斗游戏

在这里插入图片描述

  • 第一步:创建游戏角色的javabean类
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双腿。n","%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 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 getGender() {return gender;}public void setGender(char gender) {this.gender = gender;}public String getFace() {return face;}public void setFace(char gender) {//在初始化过程中使用setFace随机设置长相Random r = new Random();if (gender == '男') {//如果人物性别是男,就从boyfaces随机一个长相int index = r.nextInt(boyfaces.length);this.face = boyfaces[index];} else if (gender == '女') {//如果人物性别是男,就从grilfaces随机一个长相int index = r.nextInt(girlfaces.length);this.face = girlfaces[index];} else {this.face = "面目狰狞";}}//输出所创建角色的所有属性public void getInfoRole() {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 index = r.nextInt(attacks_desc.length);System.out.printf(attacks_desc[index], this.getName(), role.getName());System.out.println();//计算造成的伤害int hurt = r.nextInt(20) + 1;//计算被攻击者剩余血量int remainBlood = role.getBlood() - hurt;//对剩余血量做验证,如果已经为负数了,就修改为0remainBlood = remainBlood < 0 ? 0 : remainBlood;//修改被攻击者的剩余血量role.setBlood(remainBlood);//受伤情况描述if (remainBlood >= 90) {System.out.printf(injureds_desc[0], role.getName());} else if (remainBlood >= 80) {System.out.printf(injureds_desc[1], role.getName());} else if (remainBlood >= 70) {System.out.printf(injureds_desc[2], role.getName());} else if (remainBlood >= 60) {System.out.printf(injureds_desc[3], role.getName());} else if (remainBlood >= 40) {System.out.printf(injureds_desc[4], role.getName());} else if (remainBlood >= 20) {System.out.printf(injureds_desc[5], role.getName());} else if (remainBlood >= 10) {System.out.printf(injureds_desc[6], role.getName());} else {System.out.printf(injureds_desc[7], role.getName());}System.out.println();}
}
  • 创建游戏测试类进行测试
public class GameTest {public static void main(String[] args) {//1.创建2个角色Role r1 = new Role("乔峰", 100,'男');Role r2 = new Role("鸠摩智", 100,'男');r1.getInfoRole();r2.getInfoRole();//2.使用while循环实现两个角色相互攻击while (true) {//r1先攻r2r1.attack(r2);//判断被攻击者的血量,如果为0跳出输出结果、跳出循环if (r2.getBlood() == 0) {System.out.println(r1.getName() + "K.O 了" + r2.getName());break;}//r2再共计r1r2.attack(r1);//判断被攻击者的血量,如果为0跳出输出结果、跳出循环if (r1.getBlood() == 0) {System.out.println(r2.getName() + "K.O 了" + r1.getName());break;}}}
}

训练02:两个对象数组练习

  • 对象数组练习1
    • 定义数组存储3个商品对象,
    • 商品的属性:商品的id,名字,价格,库存。
    • 创建四个商品对象,并把商品对象存入到数组当中;

第一步:创建商品的javabean类

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;}@Overridepublic String toString() {return "Goods{" +"id='" + id + '\'' +", name='" + name + '\'' +", price=" + price +", count=" + count +'}';}
}

第二步:定义数组,创建4个商品对象,并存入数组

public class GoodsTest {public static void main(String[] args) {//1.动态初始化创建数组Goods[] goods = new Goods[4];//2.创建3个商品对象Goods g1 = new Goods("001","华为P40",5999.0,100);Goods g2 = new Goods("001","保温杯",239.0,50);Goods g3 = new Goods("001","小米笔记本",6999.0,10);Goods g4 = new Goods("001","苹果13手机",7999.0,59);//3.把商品对象存入数组中goods[0] = g1;goods[1] = g2;goods[2] = g3;goods[3] = g4;//3.打印数组for (int i = 0; i < goods.length; i++) {Goods good = goods[i];System.out.println(good.toString());}}
}
  • 对象数组练习2
    • 定义数组存储3部汽车对象;
    • 汽车的属性:品牌,价格,颜色;
    • 创建三个汽车对象,数据通过键盘录入而来,并把数据存入到数组当中;
  • 第一步:创建汽车的javabean类
public class Cars {private String brand;private int price;private String color;public Cars() {}public Cars(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;}@Overridepublic String toString() {return "Cars{" +"brand='" + brand + '\'' +", price=" + price +", color='" + color + '\'' +'}';}
}
  • 第二步:创建3个汽车对象,使用键盘接收汽车属性,并把3个汽车对象存入数组中
public class CarsTest {public static void main(String[] args) {//1.创建一个汽车数组Cars[] cars = new Cars[3];//2.创建3个汽车对象,使用键盘接收汽车属性,并把3个汽车对象存入数组中Scanner scanner = new Scanner(System.in);for (int i = 0; i < cars.length; i++) {//创建汽车对象,通过键盘接收汽车属性数据Cars car = new Cars();//创建汽车对象一定要写在for循环的里面,因为为我们要创建3和不同的对象System.out.println("请输入汽车的品牌:");String brand = scanner.next();car.setBrand(brand);System.out.println("请输入汽车的价格:");int price = scanner.nextInt();car.setPrice(price);System.out.println("请输入汽车的颜色:");String color = scanner.next();car.setColor(color);//把创建的汽车对象存入数组中cars[i] = car;}//3.遍历数组for (int i = 0; i < cars.length; i++) {System.out.println(cars[i].toString());}}
}
  • 以上练习中需掌握键盘输入的两套体系
  • 第一套体系:
    • nextInt();接收整数
    • nextDouble();接收小数
    • next();接收字符串
    • 遇到空格,制表符,回车就停止接受。这些符号后面的数据就不会接受了
  • 第二套体系:
    • nextLine();接收字符串
    • 可以接收空格,制表符,遇到回车才停止接受数据
  • 以上练习中还需要注意
  • Cars car = new Cars();创建汽车对象一定要写在for循环的里面,因为为我们要创建3和不同的对象;

训练03:两个对象数组练习-判断并统计

  • 对象数组练习3
    • 定义数组存储3部手机对象
    • 手机的属性:品牌,价格,颜色。
    • 要求,计算出三部手机的平均价格
  • 第一步:创建手机javabean类
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;}@Overridepublic String toString() {return "Phone{" +"brand='" + brand + '\'' +", price=" + price +", color='" + color + '\'' +'}';}
}
  • 第一步:定义数组存储3部手机对象,并计算出三部手机的平均价格
public class PhoneTest {public static void main(String[] args) {//1.创建一个大小为3的手机数组Phone[] phones = new Phone[3];//2.创建3个手机对象Phone p1 = new Phone("小米",3999,"黑色");Phone p2 = new Phone("华为",5999,"蓝色");Phone p3 = new Phone("苹果",7999,"白色");//3.把3个手机对象存入数组中phones[0] = p1;phones[1] = p2;phones[2] = p3;int sum = 0;for (int i = 0; i < phones.length; i++) {sum = sum + phones[i].getPrice();}int vag = sum / phones.length;System.out.println(vag);}
}
  • 对象数组练习4
    • 定义数组存储4个女朋友的对象
    • 女朋友的属性:姓名、年龄、性别、爱好
    • 要求1:计算出四个女朋友的平均年龄
    • 要求2:统计年龄比平均值低的女朋友有几个?并把她们的所有信息打印出来
      第一步:创建女朋友的javabean类
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;}@Overridepublic String toString() {return "GirlFriend{" +"name='" + name + '\'' +", age=" + age +", gender='" + gender + '\'' +", hobby='" + hobby + '\'' +'}';}
}

第二步:创建GirlFriendTest类实现功能

public class GirlFriendTest {public static void main(String[] args) {//1.定义数 组存储4个女朋友对象GirlFriend[] girlFriends = new GirlFriend[4];//2.创建4个女朋对象GirlFriend gf1 = new GirlFriend("小诗诗",18,"萌妹子","吃零食");GirlFriend gf2 = new GirlFriend("小丹丹",19,"萌妹子","睡觉");GirlFriend gf3 = new GirlFriend("小慧慧",20,"萌妹子","看书");GirlFriend gf4 = new GirlFriend("小艳艳",21,"萌妹子","运动");//3.将4个对象添加到数组girlFriends[0] = gf1;girlFriends[1] = gf2;girlFriends[2] = gf3;girlFriends[3] = gf4;//4.计算4个女朋友的年龄之和int sum = 0;for (int i = 0; i < girlFriends.length; i++) {sum += girlFriends[i].getAge();}//5.求平均年龄int vag = sum / girlFriends.length;System.out.println(vag);//6.统计低于平均年龄的女朋友个数,并打印出来int count = 0;for (int i = 0; i < girlFriends.length; i++) {if (girlFriends[i].getAge() < vag){count++;System.out.println(girlFriends[i].toString());}}System.out.println(count);}
}

训练05/06:复杂对象数练习-添加、遍历、修改、删除

定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,
学生对象的学号,姓名各不相同学生的属性:学号,姓名,年龄。
要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断。
要求2:添加完毕之后,遍历所有学生信息。
要求3:通过id删除学生信息,如果存在,则删除,如果不存在,则提示删除失败。
要求4:删除完毕之后,遍历所有学生信息。
要求5:查询数组id为“2”的学生,如果存在,则将他的年龄+1岁。
  • 代码示例:
public class StudentTest {/*定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同学生的属性:学号,姓名,年龄。要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断。要求2:添加完毕之后,遍历所有学生信息。要求3:通过id删除学生信息,如果存在,则删除,如果不存在,则提示删除失败。要求4:删除完毕之后,遍历所有学生信息。要求5:查询数组id为“2”的学生,如果存在,则将他的年龄+1岁。*/public static void main(String[] args) {//1.定义一个长度为3的数组Student[] students = new Student[3];//2.创建3个学生对象Student s1 = new Student(1, "张三", 23);Student s2 = new Student(2, "李四", 24);Student s3 = new Student(3, "王五", 24);//3.将3个学生对象存入数组中students[0] = s1;students[1] = s2;students[2] = s3;//4.创建一个新的学生对象Student s4 = new Student(5, "赵六", 26);//5.判断新创建学生对象学号的唯一性(设计一个方法判断新创建的学生对象的id,是否包含在老数组中)if (contains(students, s4.getId())) {//5.1已存在----提示id重复,添加失败System.out.println("学生id重复,添加失败");} else {//5.2不存在---添加新的学生对象//6.判断老数组是否已经存满数据(设计一个方法判断来老数组数据是否已经存满)int count = getCount(students);System.out.println(count);if (count == students.length) {//6.1数组已满,则对创建比原数组空间大1个的新数组,将新的学生对象存入其中;students = createArr(students);students[count] = s4;//打印数组printArr(students);} else {//6.2数组没有存满,直接将新学生对象存入其中students[count] = s4;//打印数组printArr(students);}}//要求3:通过id删除学生信息,如果存在,则删除,如果不存在,则提示删除失败。int index = getIndex(students, 5);if (index >= 0) {//如果存在,则删除students[index] = null;System.out.println("删除成功");//要求4:删除完毕之后,遍历所有学生信息。printArr(students);} else {//如果不存在,则提示学生id不存在,删除失败System.out.println("学生id不存在,删除失败");}//要求5:查询数组id为“2”的学生,如果存在,则将他的年龄+1岁。int index1 = getIndex(students, 2);if (index1 >= 0) {//id为2的学生存在,则将他的年龄+1岁Student stu = students[index1];int newAge = stu.getAge() + 1;stu.setAge(newAge);System.out.println("年龄+1岁成功");printArr(students);} else {//id为2的学生不存在System.out.println("学生信息不存在,修改失败");}}//设计一个方法获取待删除学生在数组中的位置/*1.我要个干嘛? 答:通过id获取学生对象在数组中的数组下标2.我需要什么? 答:需要数组,学生id3.方法的调用处是否需要继续使用方法的结果? 答:需要,如果存在则返回学生对象的数组下标,如果不存在返回-1*/public static int getIndex(Student[] arr, int id) {for (int i = 0; i < arr.length; i++) {if (arr[i] != null) {if (arr[i].getId() == id) {return i;}}}return -1;}//设计一个方法遍历数组元素public static void printArr(Student[] arr) {for (Student student : arr) {if (student != null) {System.out.println(student);}}}//设计一个方法,创建比原数组空间大1个的新数组/*1.我要个干嘛? 答:创建一个新数组,比老数组空间大1,并复制老数组元素到新数组中2.我需要什么? 答:需要老数组3.方法的调用处是否需要继续使用方法的结果? 答:需要,需要返回新创建的数组*/public static Student[] createArr(Student[] arr) {Student[] newStudents = new Student[arr.length + 1];for (int i = 0; i < arr.length; i++) {newStudents[i] = arr[i];}return newStudents;}//设计一个方法判断新创建的学生对象的id,是否包含在老数组中/*1.我要个干嘛? 答:判断新创建的学生对象的id,是否包含在老数组中2.我需要什么? 答:需要老数组,需要新创建学生对象的id3.方法的调用处是否需要继续使用方法的结果? 答:需要判断的结果*/public static boolean contains(Student[] arr, int id) {for (Student student : arr) {if (student != null) {if (student.getId() == id) {return true;}}}return false;}//设计一个方法判断来老数组数据已经存了几个数据/*1.我要个干嘛? 答:判断老数组数据已经存了几个数据2.我需要什么? 答:需要来老数组3.方法的调用处是否需要继续使用方法的结果? 答:需要返回已存入数据的数量*/public static int getCount(Student[] arr) {//创建一个统计变量int count = 0;//判断数组中元素的值是否为null,如果不是null则说明存在一个数据,count加1for (Student student : arr) {if (student != null) {count++;}}//循环结束后返回数组中已添加元素的个数return count;}
}
http://www.lryc.cn/news/505215.html

相关文章:

  • 电脑为什么会提示“msvcr120.dll缺失”?“找不到msvcr120.dll文件”要怎么解决?
  • huggingface NLP-微调一个预训练模型
  • 【BUG记录】Apifox 参数传入 + 号变成空格的 BUG
  • Spring AI API 介绍
  • 【MySQL】Linux使用C语言连接安装
  • 2024年第十五届蓝桥杯青少组C++国赛—割点
  • 【软件开发】做出技术决策
  • Airborne使用教程
  • WPF实现曲线数据展示【案例:震动数据分析】
  • EasyExcel 动态设置表格的背景颜色和排列
  • 【 C++11 】类的新功能
  • 防止SQL注入:PHP安全最佳实践
  • 自动化生产或质量检测准备工作杂记
  • 张志辰医生
  • CodeMirror 如何动态更新definemode
  • 舵机SG90详解
  • 程序设计考题汇总(四:SQL练习)
  • 明达IOT平台助力工业废水运维智能化
  • 深入理解 Ansible Playbook:组件与实战
  • JavaEE初阶——多线程(线程安全-锁)
  • Stable Diffusion 提示词语法
  • 【功能安全】安全确认
  • 在pycharm2024.3.1中配置anaconda3-2024-06环境
  • linux不同发行版中的主要差异
  • 概率论得学习和整理29: 用EXCEL 描述二项分布
  • C++打造局域网聊天室第九课: 客户端队列及其处理线程
  • 请求go web后端接口 java安卓端播放视频
  • XML Schema 复合类型 - 混合内容
  • 第8章 搬移特性
  • ARM/Linux嵌入式面经(五九):海尔