12.4面向对象综合练习
12.4面向对象综合练习
1.文字版格斗游戏
需求:
格斗游戏,每个游戏角色的姓名,血量,都不相同,在选定人物的时候(new对象的时候),这些信息就应该被确定下来。
举例:
程序运行之后结果为:
姓名为:乔峰 血量为:100
姓名为:鸠摩智 血量为:100
乔峰举起拳头打了鸠摩智一下,造成了XX点伤害,鸠摩智还剩下XXX点血。
鸠摩智举起拳头打了鸠摩智一下,造成了XX点伤害,乔峰还剩下XXX点血。
乔峰举起拳头打了鸠摩智一下,造成了XX点伤害,鸠摩智还剩下XXX点血。
鸠摩智举起拳头打了鸠摩智一下,造成了XX点伤害,乔峰还剩下XXX点血。
乔峰K.O.了鸠摩智
import java.util.Random;
public class Main{
public static void main(String []args){
//1.创建人物,存储名字 血量 伤害点数
Role r1 = new Role("乔峰",100,20);
Role r2 = new Role("鸠摩智",100,15);
//2.开始格斗
while(true){
r2.attack(r1);
r1.attack(r2);
if(r1.getBlood()==0){
System.out.println(r2.getName()+"OK了"+r1.getName());
break;
}
if(r2.getBlood()==0){
System.out.println(r1.getName()+"OK了"+r2.getName());
break;
}
}
}
}
import java.util.Random;
public class Role{
private String name ;
private int blood ;
private int hurt;
public Role(){}
public Role(String name, int blood, int hurt) {
this.name = name;
this.blood = blood;
this.hurt = hurt;
}
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 int getHurt() { return hurt; }
public void setHurt(int hurt) { this.hurt = hurt; }
public void attack(Role role) {
// 直接在attack方法中生成随机伤害值
Random r = new Random();
int actualHurt = r.nextInt(this.hurt + 1); // 生成0到hurt之间的随机伤害
// 计算剩余血量
int remainBlood = role.getBlood() - actualHurt;
remainBlood = remainBlood > 0 ? remainBlood : 0;
role.setBlood(remainBlood);
// 输出战斗信息
System.out.println(this.getName() + "举起拳头打了" + role.getName() + "一下,造成了" + actualHurt + "点伤害," + role.getName() + "还剩下" + role.getBlood() + "点血。");
}
}
进阶版⬇️
import java.util.Random;
public class Role{
Random r = new Random();
private String name ;
private int blood ;
private int hurt;
private char sex;
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, int hurt, char sex) {
this.name = name;
this.blood = blood;
this.hurt = hurt;
this.sex = sex;
setFace(sex);
}
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 int getHurt() { return hurt; }
public void setHurt(int hurt) {
Random r = new Random();
int actualHurt = r.nextInt(this.hurt + 1); // 生成0到hurt之间的随机伤害
this.hurt = actualHurt;
}
public char getSex() { return sex; }
public void setSex(char sex) { this.sex = sex; }
public String getFace() { return face; }
public void setFace(char sex) {
if(sex=='男'){
this.face = boyfaces[r.nextInt(boyfaces.length)];
}else{
this.face = girlfaces[r.nextInt(girlfaces.length)];
}
}
// 显示人物信息
public void ShowRoleInfor() {
System.out.println("姓名:" + this.getName() );
System.out.println("血量:" + this.getBlood() );
System.out.println("伤害点数:" + this.getHurt() );
System.out.println("性别:" + this.getSex() );
System.out.println("相貌:" + this.getFace());
}
//攻击方法
public void attack(Role role) {
//随机选择一个攻击描述
String KungFu = attacks_desc[r.nextInt(attacks_desc.length)];
//输出一个攻击的效果
System.out.printf(KungFu, this.getName(), role.getName());
System.out.println();
// 计算剩余血量
int remainBlood = role.getBlood() - this.getHurt() ;
remainBlood = remainBlood > 0 ? remainBlood : 0;
role.setBlood(remainBlood);
//受伤的描述
//血量> 90 0索引的描述
//80 ~ 90 1索引的描述
//70 ~ 80 2索引的描述
//60 ~ 70 3索引的描述
//40 ~ 60 4索引的描述
//20 ~ 40 5索引的描述
//10 ~ 20 6索引的描述
//小于10的 7索引的描述
//输出一个受伤的效果
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();
}
}
import java.util.Random;
public class GameTest {
public static void main(String []args){
Random r = new Random();
//1.创建人物,存储名字 血量 伤害点数
Role r1 = new Role("乔峰",100,20,'男');
Role r2 = new Role("鸠摩智",100,15,'男');
//2.显示人物信息
r1.ShowRoleInfor();
System.out.println();
r2.ShowRoleInfor();
System.out.println("---------------------");
//2.开始格斗
while(true){
r2.attack(r1);
r1.attack(r2);
if(r1.getBlood()==0){
System.out.println(r2.getName()+"OK了"+r1.getName());
break;
}
if(r2.getBlood()==0){
System.out.println(r1.getName()+"OK了"+r2.getName());
break;
}
}
}
}
2.对象数组练习
需求:
定义数组存储3个商品对象。
商品的属性:商品的id,名字,价格,库存。
创建三个商品对象,并把商品对象存入到数组当中。
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;}
}
public class GoodsTest {
public static void main(String[] args) {
//1.创建一个数组
Goods[] goodsArr = new Goods[3];
//2.创建三个商品对象
//ctrl+p 查看构造方法的参数
Goods g1 = new Goods("001","商品1",100,10);
Goods g2 = new Goods("002","商品2",200,20);
Goods g3 = new Goods("003","商品3",300,30);
//3.将商品对象添加到数组中
goodsArr[0] = g1;
goodsArr[1] = g2;
goodsArr[2] = g3;
//4.遍历数组,打印每个商品的信息
for (int i = 0; i < goodsArr.length; i++) {
Goods goods = goodsArr[i];
System.out.println(goods.getId() + "," + goods.getName() + "," + goods.getPrice()+","+goods.getCount());
}
}
}
进阶⬇️:
import java.util.Scanner;
public class CarGoodsTest {
public static void main(String[] args) {
CarGoods[] cars = new CarGoods[3];
Scanner sc = new Scanner(System.in);
for(int i = 0; i < cars.length; i++){
CarGoods c = new CarGoods();
System.out.println("请输入第" + (i+1) + "辆车的品牌");
c.setBrand(sc.next());
System.out.println("请输入第" + (i+1) + "辆车的价格");
c.setPrice(sc.nextDouble());
System.out.println("请输入第" + (i+1) + "辆车的颜色");
c.setColor(sc.next());
cars[i] = c;
}
for(int i = 0; i < cars.length; i++){
System.out.println("第" + (i+1) + "辆车的品牌是" + cars[i].getBrand() + ",价格是" + cars[i].getPrice() + ",颜色是" + cars[i].getColor() );
}
}
}
public class CarGoods {
private String brand;
private double price;
private String color;
public CarGoods() {
}
public CarGoods(String brand, double price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
/**
* 获取
* @return brand
*/
public String getBrand() {
return brand;
}
/**
* 设置
* @param brand
*/
public void setBrand(String brand) {
this.brand = brand;
}
/**
* 获取
* @return price
*/
public double getPrice() {
return price;
}
/**
* 设置
* @param price
*/
public void setPrice(double price) {
this.price = price;
}
/**
* 获取
* @return color
*/
public String getColor() {
return color;
}
/**
* 设置
* @param color
*/
public void setColor(String color) {
this.color = color;
}
}
3.复杂的对象数组操作
定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同。
学生的属性:学号,姓名,年龄。
要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断。
要求2:添加完毕之后,遍历所有学生信息。
要求3:通过id删除学生信息
如果存在,则删除,如果不存在,则提示删除失败。
要求4:删除完毕之后,遍历所有学生信息。
要求5:查询数组id为“heima002”的学生,如果存在,则将他的年龄+1岁
public class Student{
private int studentId;
private String name;
private int age;
public Student(){}
public Student(int studentId , String name , int age){
this.studentId = studentId;
this.name = name;
this.age = age;
}
/**
* 获取
* @return studentId
*/
public int getStudentId() {
return studentId;
}
/**
* 设置
* @param studentId
*/
public void setStudentId(int studentId) {
this.studentId = studentId;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{studentId = " + studentId + ", name = " + name + ", age = " + age + "}";
}
}
public class StudentTest {
public static void main(String[] args) {
//1.创建一个数组用来存储学生对象
Student[] arr = new Student[3];
//2.创建学生对象并添加到数组当中
Student stu1 = new Student(1, "张三", 23);
Student stu2 = new Student(2, "李四", 24);
Student stu3 = new Student(3,"王五",20);
//3.把学生对象添加到数组当中
arr[0] = stu1;
arr[1] = stu2;
arr[2] = stu3;
//----------------------------------
//要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断。
//要求2:添加完毕之后,遍历所有学生信息。
Student stu4 = new Student(4, "赵六", 26);
//唯一性判断(优先)
//已存在 --- 不用添加
//不存在 --- 就可以把学生对象添加进数组
//添加
//1.数组已经存满---只能创建一个新数组,新数组的长度=老数组+1;把老数组的元素,拷贝到新数组当中
//2.数组没有存满---直接添加
boolean flag = stuContain(arr , stu4.getStudentId());
if(flag){
//已存在 --- 不用添加
System.out.println("当前id重复,请修改id后再进行添加");
}else{
//不存在 --- 就可以把学生对象添加进数组
int count = getCount(arr);
if (count == arr.length) {
// 已经存满
arr = creatNewArr(arr); // 用新数组替换原数组
arr[count] = stu4;
printArr(arr);
} else {
// 没有存满
arr[count] = stu4;
printArr(arr);
}
}
}
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.getStudentId() + ",姓名:" + stu.getName() + ",年龄:" + stu.getAge());
// 除了最后一个有效元素外,其他元素后都输出分隔线
if (i < arr.length - 1 && arr[i + 1] != null) {
System.out.println("---------------------");
}
//arr[i + 1] != null
//这个条件检查当前元素的下一个元素(索引为 i+1)是否 不为 null
//也就是说,下一个位置确实存在有效的学生对象
}
}
}
public static Student[] creatNewArr(Student[] arr){
Student[] newArr = new Student[arr.length +1];
for(int i = 0 ; i<arr.length ; i++){
newArr[i] = arr[i];
}
return newArr;
}
public static int getCount(Student[] arr){
int count = 0;
for(int i = 0 ; i < arr.length ; i++){
if(arr[i]!=null){
count++;
}
}
return count;
}
public static boolean stuContain(Student[] arr ,int id){
for(int i = 0 ; i < arr.length ; i++){
Student stu = arr[i];
//在stuContain方法中,直接访问arr[i].getStudentId(),
//但没有检查arr[i]是否为null,当数组中有null元素时会抛出空指针异常。
if (stu != null && stu.getStudentId() == id){
return true;
}
}return false;
}
}
public class StudentTest2 {
public static void main(String[] args) {
//1.创建一个数组用来存储学生对象
Student[] arr = new Student[3];
//2.创建学生对象并添加到数组当中
Student stu1 = new Student(1, "张三", 23);
Student stu2 = new Student(2, "李四", 24);
Student stu3 = new Student(3, "王五", 20);
//3.把学生对象添加到数组当中
arr[0] = stu1;
arr[1] = stu2;
arr[2] = stu3;
//----------------------------------
//要求3:通过id删除学生信息,如果存在,则删除,如果不存在,则提示删除失败。
int index = getIndex(arr,2);
if(index >= 0){
arr[index] = null;
System.out.println("删除成功");
}else{
System.out.println("删除失败,并不存在此id的学生");
}
//要求4:删除完毕之后,遍历所有学生信息。
printArr(arr);
}
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.getStudentId() + ",姓名:" + stu.getName() + ",年龄:" + stu.getAge());
// 除了最后一个有效元素外,其他元素后都输出分隔线
if (i < arr.length - 1 && arr[i + 1] != null) {
System.out.println("---------------------");
}
//arr[i + 1] != null
//这个条件检查当前元素的下一个元素(索引为 i+1)是否 不为 null
//也就是说,下一个位置确实存在有效的学生对象
}
}
}
public static int getIndex(Student[] arr, int id) {
for (int i = 0; i < arr.length; i++) {
Student stu = arr[i];
if (stu != null) {
if (stu.getStudentId() == id) {
return i;
}
}
}
return -1;
}
}
public class StudentTest3 {
public static void main(String[] args) {
//1.创建一个数组用来存储学生对象
Student[] arr = new Student[3];
//2.创建学生对象并添加到数组当中
Student stu1 = new Student(1, "张三", 23);
Student stu2 = new Student(2, "李四", 24);
Student stu3 = new Student(3, "王五", 20);
//3.把学生对象添加到数组当中
arr[0] = stu1;
arr[1] = stu2;
arr[2] = stu3;
//要求5:查询数组id为“3”的学生,如果存在,则将他的年龄+1岁
int index = getIndex(arr,3);
if(index >= 0){
addAge(arr,index);
System.out.println("年龄增加成功");
}else{
System.out.println("年龄增加失败,并不存在此id的学生");
}
//完毕之后,遍历所有学生信息。
printArr(arr);
}
public static int getIndex(Student[] arr, int id) {
for (int i = 0; i < arr.length; i++) {
Student stu = arr[i];
if (stu != null) {
if (stu.getStudentId() == 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.getStudentId() + ",姓名:" + stu.getName() + ",年龄:" + stu.getAge());
// 除了最后一个有效元素外,其他元素后都输出分隔线
if (i < arr.length - 1 && arr[i + 1] != null) {
System.out.println("---------------------");
}
//arr[i + 1] != null
//这个条件检查当前元素的下一个元素(索引为 i+1)是否 不为 null
//也就是说,下一个位置确实存在有效的学生对象
}
}
}
public static void addAge(Student[] arr, int index){
Student stu = arr[index];
stu.setAge(stu.getAge() +1);
}
}
4.键盘录入
第一套体系:
nextInt();接收整数
nextDouble();接收小数
next();接收字符串
遇到空格,制表符,回车就停止接受。这些符号后面的数据就不会接受了
第二套体系:
nextLine();接收字符串
可以接收空格,制表符,遇到回车才停止接受数据

浙公网安备 33010602011771号