day_09:面向对象综合训练
souf输出语句
两部分参数:
- 第一部分:要输出的内容(%s占位符)
- 第二部分:填充的数据
键盘录入
键盘录入共有两套体系:
- 第一套体系:
nextlnt();接收整数nextDoub1e();接收小数next();接收字符串
遇到空格,制表符,回车就停止接受。这些符号后面的数据就不会接受了。 - 第二套体系:
nextLine();接收字符串
可以接收空格,制表符,遇到回车才停止接受数据
弊端
键盘录入的两套体系不能混用的;; 先用nextlnt, 再用nextLine会导致下面的nextLine接受不到数据。
一、文字版格斗游戏
格斗游戏,每个游戏角色的姓名,血量,都不相同,在选定人物的时候 ( new 对象的时候 ) ,这些信息 就应该被确定下来。
java
public class gameTest {
static void main(String[] args) {
Role r1 = new Role("廖玉洁", 100,'女');
Role r2 = new Role("彭茹芳", 100,'女');
r1.showRoleInfo();
System.out.println();
r2.showRoleInfo();
System.out.println();
while (true) {
r1.attack(r2);
//判断剩余血量
if (r2.getBlood() == 0) {
System.out.println("游戏结束了:"+r1.getName() + " 杀死了了 " + r2.getName());
break;
}
r2.attack(r1);
//判断剩余血量
if (r1.getBlood() == 0) {
System.out.println("游戏结束了:"+r2.getName() + " 杀死了了 " + r1.getName());
break;
}
}
}
}java
public class Role {
private String name;
private int blood;
private char gender;
private String face;
//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(String name, int blood, char gender) {
this.name = name;
this.blood = blood;
this.gender = gender;
//随机长相
setFace(gender);
}
//空参构造
public Role() {
}
//get和set方法
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 == '男') {
//从boyfaces随机
int index = r.nextInt(boyfaces.length);
this.face = boyfaces[index];
} else if (gender == '女') {
//从girlfaces随机
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 getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
//定义攻击方法,方法调用者攻击参数
public void attack(Role 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());
System.out.println();
//随机造成的伤害
int hurt = r.nextInt(20) + 1;
//剩余血量计算
int remainBlood = Role.getBlood() - hurt;
//对血量进行非负验证
remainBlood = remainBlood < 0 ? 0 : remainBlood;
//修改被攻击血量
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 && 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();
}
//角色信息展示方法
public void showRoleInfo() {
System.out.println("姓名:" + getName());
System.out.println("血量:" + getBlood());
System.out.println("性别:" + getGender());
System.out.println("长相:" + getFace());
}
}二、对象数组练习
对象数组 1
定义数组存储 3 个商品对象。 商品的属性,商品的 id ,名字,价格,库存。 创建三个商品对象,并把商品对象存入到数组当中。
java
public class GoodsTest {
static void main(String[] args) {
//创建一个数组
Goods[] arr = new Goods[3];
//创建对象
Goods good1 = new Goods("001", "华为P40", 5999, 100);
Goods good2 = new Goods("002", "保温杯", 200, 50);
Goods good3 = new Goods("003", "枸杞", 200, 70);
//添加进入数组
arr[0] = good1;
arr[1] = good2;
arr[2] = good3;
for (int i = 0; i < arr.length; i++) {
Goods good = arr[i];
System.out.println(good.getId() + "," + good.getName() + "," + good.getPrice() + "," + good.getCount());
}
}
}java
public class Goods {
private String id;
private String name;
private double price;
private int count;
//全参构造
public Goods(String id, String name, double price, int count) {
this.id = id;
this.name = name;
this.price = price;
this.count = count;
}
//空参构造
public Goods() {
}
//get和sey方法
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;
}
}对象数组 2
定义数组存储 3 部汽车对象。 汽车的属性:品牌,价格,顔色。 创建三个汽车对象,数据通过键盘录入而来,并把数据存入到数组当中。
java
import java.util.Scanner;
public class CarTest {
static void main(String[] args) {
//创建一个数组
Car[] arr = new Car[3];
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
//创建汽车对象,数据来源键盘录入
for (int i = 0; i < arr.length; i++) {
//键盘录入汽车品牌
System.out.println("请输入第" + (i + 1) + "辆汽车的品牌:");
String brand = sc.next();
System.out.println("请输入第" + (i + 1) + "辆汽车的价格:");
int price = sc.nextInt();
System.out.println("请输入第" + (i + 1) + "辆汽车的颜色:");
String color = sc.next();
//创建汽车对象
Car car = new Car(brand, price, color);
//将汽车对象放入数组中
arr[i] = car;
}
//输出结果
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i].getBrand() + "," + arr[i].getPrice() + "," + arr[i].getColor());
}
}
}java
public class Car {
private String brand;
private int price;
private String 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;
}
public Car(String brand, int price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
public Car() {
}
}对象数组3
定义数组存储 3 部手机对象。 手机的属性:品牌,价格,颜色。 要求,计算出三部手机的平均价格
java
public class PhoneTest {
static void main(String[] args) {
//创建一个数组
Phone[] arr = new Phone[3];
//创建手机对象
Phone p1 = new Phone("小米", 1999, "蓝色");
Phone p2 = new Phone("华为", 2999, "白色色");
Phone p3 = new Phone("小米", 9999, "黑色");
//手机对象放入数组中
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 += phone.getPrice();
}
//输出结果
System.out.println(sum * 1.0 / arr.length);
}
}java
public class Phone {
private String brand;
private int price;
private String color;
//get和set方法
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;
}
//全参构造
public Phone(String brand, int price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
//空参构造
public Phone() {
}
}对象数组4
定义数组存储 4 个女朋友的对象 女朋友属性:姓名、年龄、性别、爱好 要求 1 :计算出四女朋友的平均年龄 要求 2 :统计年龄比平均值低的女朋友有几个?并把她们的所有信息打印出来。
java
public class GirlFriendTest {
static void main(String[] args) {
//定义数组
GirlFriend[] arr = new GirlFriend[4];
//创建女朋友对象
GirlFriend gf1 = new GirlFriend("廖玉洁", 19, '女', "化妆");
GirlFriend gf2 = new GirlFriend("彭茹芳", 19, '女', "唱歌");
GirlFriend gf3 = new GirlFriend("肖清育", 18, '女', "跳舞");
GirlFriend gf4 = new GirlFriend("王杨", 124, '男', "蓝球");
//将对象放入数组中
arr[0] = gf1;
arr[1] = gf2;
arr[2] = gf3;
arr[3] = gf4;
//定义变量存储求和
int sum = 0;
//遍历数组并获得对象
for (int i = 0; i < arr.length; i++) {
GirlFriend gf = arr[i];
//求和
sum += gf.getAge();
}
//计算平均值
double avg = sum * 1.0 / arr.length;
//定义变量存储年龄低于平均值的数量
int count = 0;
//对年龄进行判断
for (int i = 0; i < arr.length; i++) {
if (arr[i].getAge() < avg) {
count++;
}
}
//输出结果
System.out.println("平均年龄为:" + avg);
System.out.println("低于平均值的有:" + count);
}
}java
public class GirlFriend {
private String name;
private int age;
private char gender;
private String hobby;
//getheset方法
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 char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
//全参构造
public GirlFriend(String name, int age, char gender, String hobby) {
this.name = name;
this.age = age;
this.gender = gender;
this.hobby = hobby;
}
//空参构造
public GirlFriend() {
}
}对象数组5
定义一个长度为 3 的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同。 学生的属性:学号,姓名,年龄。 要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断; 要求2:添加完毕之后,遍历所有学生信息; 要求3:通过id删除学生信息,如果存在,则删除,如果不存在,则提示删除失败; 要求4:删除完毕之后,遍历所有学生信息; 要求5:查询数组 id 为"2"的学生,如果存在,则将他的年龄+1岁。
java
public class StudentTest {
static void main(String[] args) {
//定义数组存储Student对象
Student[] arr = new Student[3];
//创建学生对象
Student s1 = new Student(1, "廖玉洁", 23);
Student s2 = new Student(2, "彭茹芳", 23);
Student s3 = new Student(3, "肖清育", 23);
//添加学生对象到数组中
arr[0] = s1;
arr[1] = s2;
//要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断
Student s4 = new Student(4, "qing", 23);
if (contains(s4, arr)) {
int count = getCount(arr);
if (count == arr.length) {
//存满
//创建新的数组
Student[] newArr = creatNewArr(arr);
//添加新对象
newArr[count] = s4;
//要求2:添加完毕之后,遍历所有学生信息
printArr(newArr);
} else {
//未满
arr[count] = s4;
//要求2:添加完毕之后,遍历所有学生信息
printArr(arr);
}
} else {
System.out.println("当前id重复,请修改id后重试!");
}
//要求3:通过id删除学生信息,如果存在,则删除,如果不存在,则提示删除失败
int index = getIndex(3, arr);
//判断id存在
if (index > 0) {
arr[index] = null;
//遍历数组
printArr(arr);
} else {
System.out.println("该id不存在");
}
//要求5:查询数组 id 为"2"的学生,如果存在,则将他的年龄+1岁
int index1 = getIndex(2, arr);
//判断索引
if (index1 > 0) {
//修改年龄
Student stu = arr[index1];
arr[index1].setAge(stu.getAge() + 1);
//遍历数组
printArr(arr);
} else {
System.out.println("当前id不存在");
}
}
//定义方法判断id是否唯一
public static boolean contains(Student student, Student[] arr) {
//定义变量存储是否唯一,true为唯一,false为不唯一
boolean flag = true;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
if (arr[i].getId() == student.getId()) {
flag = false;
}
}
}
return flag;
}
//定义方法判断数组存储了几个
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 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 void printArr(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());
}
}
}
//找到id在数组中的索引
public static int getIndex(int id, Student[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
if (arr[i].getId() == id) {
return i;
}
}
}
return -1;
}
}java
public class Student {
private int id;
private String name;
private int age;
//get和set方法
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 Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
//空参构造
public Student() {
}
}