面向对象(综合案例)
主程序:
public class Day9 {
public static void main(String[] args) {
// 一、文字版格斗游戏
//1.创建第一个角色
Role r1 = new Role("乔峰",100);
//2.创建第二个角色
Role r2 = new Role("鸠摩智",100);
int i = 0;
while(true) {
r1.attack(r2);
if(r2.getBlood() == 0){
System.out.println(r1.getName()+"K.O了"+r2.getName());
break;
}
r2.attack(r1);
if(r1.getBlood() == 0){
System.out.println(r2.getName()+"K.O了"+r1.getName());
break;
}
i++;
//输出语句:souf
// 第一部分:要输出的内容%s(占位符)
// 第二部分:要填充的数据
System.out.printf("第%d回合 \n",i);
}
// 二、对象数组
//练习一 定义输出存储3个商品的对象。
// 商品的属性:商品的id,名字,价格,库存。
// 创建三个商品对象,并把商品对象存入到数组当中。
//
//1.创建一个数组
Goods[] arr1 = new Goods[3];
//2.创建三个商品对象,变量类型为自己定义的Goods
Goods g1 = new Goods("001","华为P40",5999.0,100);
Goods g2 = new Goods("002","保温杯",227.0,50);
Goods g3 = new Goods("003","枸杞",12.7,70);
//3.把商品添加到数组中,将自己定义的Goods存储进数组
arr1[0] = g1;
arr1[1] = g2;
arr1[2] = g3;
int sum = 0;
//4.遍历
for (int i1 = 0; i1 < arr1.length; i1++) {
//i 索引,arr[i]元素
Goods goods = arr1[i1];
System.out.println(goods.getId()+","+goods.getName()+","+goods.getPrice()+","+goods.getCount());
sum += goods.getPrice();
}
System.out.println("平均价格为:"+((double)(sum) / (arr1.length))); // 转换为小数的方法1
System.out.printf("平均价格为:%.2f\n",((sum * 1.0 / (arr1.length)))); // 转换为小数的方法2
int count = 0;
for (int i1 = 0; i1 < arr1.length; i1++) {
Goods goods = arr1[i1];
if(goods.getPrice() < ((sum * 1.0 / (arr1.length)))){
count ++;
}
}
System.out.printf("有%d个低于平均值\n",count);
// 练习二 定义数组存储三部汽车对象。
// 汽车的属性:品牌,价格,颜色
// 创建三个汽车对象,数据通过键盘录入而来,并把数据存入数组中
Cars[] arr2 = new Cars[3];
Scanner sc = new Scanner(System.in);
for (int i1 = 0; i1 < arr2.length; i1++) {
//每次循环都需要定义一个新类,如果在只有一个类,则都指向同一个地址。
Cars c = new Cars();
//录入品牌
System.out.println("请输入第"+(i1 + 1)+"辆车的品牌");
c.setBrand(sc.next());
//录入价格
System.out.println("请输入第"+(i1 + 1)+"辆车的价格");
c.setPrice(sc.nextDouble());
//录入颜色
System.out.println("请输入第"+(i1 + 1)+"辆车的颜色");
c.setColour(sc.next());
//存储到数组中
arr2[i1] = c;
}
/*键盘录入:
//第一套体系:
//nextInt();
//nextDouble();
//next();
//遇到空格,制表符,回车就停止接受。这些符号后面的数据就不会接受了
//虽然不再接受,但是后面的数据会存在内存当中,下一次调用时优先接受内存中的数据
//第二套体系:
//nextLine();接收字符串
//可以接收空格,制表符,遇到回车才停止接受数据。
//两套体系不建议混用,可能会出现非常难发现的bug!*/
for (int i1 = 0; i1 < arr2.length; i1++) {
//i 索引,arr[i]元素
Cars cars = arr2[i1];
System.out.println(cars.getBrand()+","+cars.getPrice()+","+cars.getColour());
}
// 练习三:学生信息
/*定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同
学生的属性:学号,姓名,年龄
要求1:再次添加一个学生对象时,进行学号的唯一性判断
要求2:添加完毕之后,遍历所有学生信息
要求3.通过id删除学生信息
如果存在,则删除,如果不存在,则提示删除失败
要求4.删除完毕之后,遍历所有学生信息
要求5.查询数组id为“heima002”的学生,如果存在,则将他的年龄+1岁 */
//创建一个数组用来存储学生对象
Student[] arr3 = new Student[4];
//创建学生对象并添加到数组当中
Student stu1 = new Student(001,"张三",13);
Student stu2 = new Student(002,"李四",14);
Student stu3 = new Student(003,"王五",15);
arr3[0] = stu1;
arr3[1] = stu2;
arr3[2] = stu3;
//要求1:再次添加一个学生对象时,进行学号的唯一性判断
//读题拆解
Student stu4 = new Student(004,"赵六",16);
//stu4添加到数组当中,如果原数组已满,所以要新建数组用来存放所有数据
//唯一性判断要在添加之前,如果不存在再添加,创建一个新方法来判断
//同时还需创建一个方法来判断数组是否已满。
if(contains(arr3,stu4.getId())){
System.out.printf("id已经存在!\n");
}else{
int count1 = getCount(arr3);
if(count == arr3.length) {
System.out.printf("数组没有存满,直接添加\n");
//getCount获取的位置表示有了多少元素,同时表示下一次添加的位置
arr3[count1] = stu4;
printArr(arr3);
}else{
//已经存满了,要创建新数组,下面新建一个方法
Student[] arr4 = createNewArr(arr3);
arr4[count1] = stu4;
printArr(arr4);
}
}
//要求2:添加完毕之后,遍历所有学生信息
//因为前面可能产生了新的数组,所以使用一个新方法来遍历数组,这里来获取需要遍历的数组
//在上方添加完成后直接遍历
//要求3:通过id删除学生信息
// 如果存在,则删除,如果不存在,则提示删除失败
//首先需要找到id,创建新方法
int index = getIndex(003,arr3);
System.out.printf("id所在位置为%d\n",index);
if (index >= 0){
for (int i1 = index; i1 < arr3.length - 1; i1++) {
arr3[i1] = arr3[i1+1];
}
}else{
System.out.println("未能删除该id");
}
printArr(arr3);
//剩下要求5思路:遍历查询,找到则setAge()
}
public static boolean contains(Student[] arr,int id){
//1.我要干嘛? 唯一性判断
//2.我干这件事,需要什么才能完成? 数组,id
//调用处是否需要继续使用方法的结果? 返回boolean
for (int i = 0; i < arr.length; i++) {
//依次获取到里面的每一个学生对象
Student stu = arr[i];
//获取数组中学生对象的id
if(stu != null ){// 防止出错
int sid = stu.getId();
//比较
if(sid == id) {
return true;
}
}
}
//若循环结束后没有找到,则代表id不存在。
return false;
}
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[] createNewArr(Student[] arr){
Student[] newArr = new Student[arr.length + 1];
//循环,copy新数组
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());
}
}
}
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;
}
}
Cars.java
public class Cars {
private String Brand;
private double Price;
private String Colour;
public String getBrand() {
return Brand;
}
public void setBrand(String brand) {
Brand = brand;
}
public double getPrice() {
return Price;
}
public void setPrice(double price) {
Price = price;
}
public String getColour() {
return Colour;
}
public void setColour(String colour) {
Colour = colour;
}
public Cars(String brand, double price, String colour) {
Brand = brand;
Price = price;
Colour = colour;
}
public Cars() {
}
}
Goods.java
public class Goods {
private String id;
private String name;
private double price;
private int 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 Goods(String id, String name, double price, int count) {
this.id = id;
this.name = name;
this.price = price;
this.count = count;
}
public Goods() {
}
}
Role.java
import java.util.Random;
public class Role {
private String name;
private int blood;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Role(String name, int blood) {
this.name = name;
this.blood = blood;
}
public Role() {
}
public int getBlood() {
return 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 hurt = r.nextInt(20) + 1;
int remainBlood = role.getBlood() - hurt;
remainBlood = remainBlood < 0 ? 0:remainBlood;
role.setBlood(remainBlood);
//this表示方法的调用者
System.out.println(this.getName() + "举起拳头,打了" +
role.getName() + "一下,"+"造成了" + hurt + "点伤害,还剩下"+ remainBlood + "点血");
}
}
Student.java
public class Student {
private int id;
private String name;
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() {
}
private int age;
}