Java基础-面向对象-案例+讲解-文字版本格斗游戏-对象数组

目录

知识点部分:

实际创建过程:

封装:

案例1:文字版格斗游戏

案例:对象数组1

案例:对象数组2

案例:对象数组3

案例:对象数组4(上难度)

 第一二问题的代码:

第三四问的代码:

第五问的代码:

知识点部分:

必须先设计类才能获取对象。

实际创建过程:

面向对象的三大特征:封装,继承,多态。

封装:

告诉我们,如何正确设计对象和属性方法。

对象代表什么,就得封装对应的数据,并提供数据对应的行为。

人关门这个场景:人给门一个作用力,门自己把自己关了,所以关门这个方法一定是门的方法,而不是人的方法。

成员变量和局部变量

(就近原则)

加上this变成成员变量

%s讲解

案例1:文字版格斗游戏

package duixiang.user;

public class playgamename {
    public static void main(String[] args) {
    playgame r1=new playgame("乔峰",100);
  playgame r2=new playgame("丽华",100);
//       因为不知道结束条件,用while循环
        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;
            }
        }
    }
}
package duixiang.user;

import java.util.Random;

public class playgame {
    private String name;
    private int blood;
    //空参
    public playgame() {

    }
    //全参
    public playgame(String name, int blood) {
        this.name = name;
        this.blood = blood;
    }
    //针对每个,对应相应方法
    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;
    }
    //    定义一个方法,攻击其他人
/*playgame role 部分分为两部分:

    playgame: 这是参数的类型,表示该参数必须是 playgame 类的一个实例。
    role: 这是参数的名称,你可以在方法体内使用这个名称来引用传递进来的 playgame 对象。*/
    public void attack(playgame role) {
        Random R = new Random(); // 创建一个Random对象,用于生成随机数
        int hurt = R.nextInt(20) + 1; // 生成1到20之间的随机伤害值
        int remainBoold = role.getBlood() - hurt; // 计算目标剩余的血量
        remainBoold = remainBoold < 0 ? 0 : remainBoold; // 如果剩余血量小于0,则设为0
        role.setBlood(remainBoold); // 更新目标的血量
        System.out.println(this.getName() + "打了" + role.getName() +
                "造成了" + hurt + "点伤害"+role.getName()+"还剩血量值为" + remainBoold); // 输出攻击信息
    }}

运行结果

案例:对象数组1

package duixiang.Goods;

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 duixiang.Goods;

public class Goodstext {
    public static void main(String[] args) {
        //创建一个数组,用于存储3个商品对象
        Goods[] arr = new Goods[3];
        //用动态初始化创建三个商品对象
        //快捷键ctl +p查看需要传递的数
        Goods g1=new Goods("001","华为",5666.0,100);
        Goods g2=new Goods("002","保温杯",277.0,20);
        Goods g3=new Goods("003","枸杞",6.0,40);
        //把商品添加到数组中
        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

package duixiang.Car;

import org.w3c.dom.html.HTMLOptGroupElement;

public class Car{
    private String brand;
    private String price;
    private int color;


    public Car() {
    }

    public Car(String brand, String price, int 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 String getPrice() {
        return price;
    }

    /**
     * 设置
     * @param price
     */
    public void setPrice(String price) {
        this.price = price;
    }

    /**
     * 获取
     * @return color
     */
    public int getColor() {
        return color;
    }

    /**
     * 设置
     * @param color
     */
    public void setColor(int color) {
        this.color = color;
    }


}
package duixiang.Car;

import java.util.Scanner;

public class cartext {
    public static void main(String[] args) {
        Car[] arrcar = new Car[3];
        //键盘录入汽车对象
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arrcar.length; i++) {
            Car car = new Car();

            System.out.printf("请输入第%s汽车的品牌",i+1);
            String brand =sc.next();
            car.setBrand(brand);
            System.out.printf("请输入第%s汽车的价格",i+1);
            int  price =sc.nextInt();
            car.setPrice(price);
            System.out.printf("请输入第%s汽车的颜色",i+1);
            String color =sc.next();
            //以上是一个汽车对象,要把汽车对象加入数组中
            arrcar[i]=car;
        }
        for (int i = 0; i < arrcar.length; i++) {
            System.out.printf("第%s辆车",i+1);
            System.out.println();
            System.out.println(arrcar[i].getBrand());
            System.out.println(arrcar[i].getPrice());
            System.out.println(arrcar[i].getColor());
            System.out.println();

        }

    }
}

运行结果

案例:对象数组3

package duixiang.phone;

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;
    }

    /**
     * 获取
     * @return brand
     */
    public String getBrand() {
        return brand;
    }

    /**
     * 设置
     * @param brand
     */
    public void setBrand(String brand) {
        this.brand = brand;
    }

    /**
     * 获取
     * @return price
     */
    public int getPrice() {
        return price;
    }

    /**
     * 设置
     * @param price
     */
    public void setPrice(int price) {
        this.price = price;
    }

    /**
     * 获取
     * @return color
     */
    public String getColor() {
        return color;
    }

    /**
     * 设置
     * @param color
     */
    public void setColor(String color) {
        this.color = color;
    }


}
package duixiang.phone;

import java.util.Scanner;

public class phonetext {
    public static void main(String[] args) {
        phone[] phones = new phone[3];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < phones.length; i++) {
            //每一次遍历是生成一个phone对象
            phone ph = new phone();
            //键盘录入手机的品牌价格颜色
            System.out.printf("请输入第%s个手机的品牌", (i + 1));
            String brand = sc.next();
            ph.setBrand(brand);

            System.out.printf("请输入第%s个手机的价格", (i + 1));
            int price = sc.nextInt();
            ph.setPrice(price);

            System.out.printf("请输入第%s个手机的颜色", (i + 1));

           String color = sc.next();
            System.out.println();
           ph.setColor(color);
           phones[i] = ph;

        }
        for (int i = 0; i < phones.length; i++) {
            System.out.printf("第%s个手机的品牌价格颜色分别为", (i + 1));
            System.out.println(phones[i].getBrand()+","+phones[i].getPrice()+","+phones[i].getColor());
        }
       int x=0;
        for (int i = 0; i < phones.length; i++) {

             x=x+phones[i].getPrice();
             if (i+1==phones.length){
                 System.out.println("总价格为"+x+"元");
             }
        }
        int avg=x/phones.length;
        System.out.println("平均价格为"+avg+"元");
    }
}

运行结果

案例:对象数组4(上难度)

 第一二问题的代码:

package duixiang.student;

public class student {
    private int id;
    private String name;
    private int age;

    public student() {
    }

    public student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     * @return id
     */
    public int getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * 获取
     * @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;
    }


}
package duixiang.student;

public class studenttext {
    public static void main(String[] args) {
        //完成题目基本要求,定义长度为3的数组,并存储1-3名学生对象作为初始数据
    student[] arr = new student[3];
        student st1 = new student(1,"李华",21);
        student st2 = new student(2,"张三",23);
        student st3 = new student(3,"王二",22);
        arr[0]=st1;
        arr[1]=st2;
        arr[2]=st3;

    //1.在添加一个学生对象,并判断学号的唯一性。
        //要判断学号的唯一性,已存在那就不用添加,不存在就可以把学生对象添加进数组。
        //在添加的时候要判断,数组已存满,就要重新创建一个数组,新数组的长度=老数组+1
        student st4=new student(4,"wj",20);
        //写一个contains方法,判断id
        //调用contains方法
        boolean flag=contains(arr, st4.getId());
        if(flag){
        //已经存在
            System.out.println("当前id重复请修改id:"+st4.getId());

        }else {
            //不存在,需要添加st4
            //写一个getcount方法,判断数组中存了多少,调用使用
            int count = getcount(arr);
            if(count==arr.length){
                //已经存满,需要新的createnewarr方法,用来创建一个新数组,长度老数组+1,然后把老数组元素拷贝到新数组当中
            student[] newarr = createNewarr(arr);
                newarr[count]=st4;
                /
                //2.添加完毕后,遍历所有学生信息
                printArr(newarr);
            }else {
                arr[count]=st4;
                /
                //2.添加完毕后,遍历所有学生信息
                printArr(arr);
            }
        }
    }
    //这个方法要做啥?判断id唯一性
    //我干这件事情,需要什么才能完成?数组id
    //调用处是否要用返回值结果?必须返回需要
    public static boolean contains(student[] arr,int id){
        for(int i=0;i<arr.length;i++){
            student st=arr[i];
            if(st!=null){
                int stid=st.getId();
                if(id==stid){
                    return true;
                }
            }

        }
        //循环结束后在returnfalse,如果紧跟if后面,会有问题。
        return false;
    }
    //判断数组中已经存了多少,是否存满
    public static int getcount(student[] arr){
        //定义一个计数器用来统计数组中有多少个数
        int count=0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                count++;

            }
        }
        //循环结束返回count,即数组有多少个
        return count;
    }
    //创建新数组,老数组拷贝到新数组中
    public static student[] createNewarr(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++){
            student stu=arr[i];
            if(stu!=null){
                System.out.println(stu.getId()+" ,"+stu.getName()+" ,"+stu.getAge());
            }

        }
    }
    找id在数组中的索引
}

一二问运行结果:

去掉王二后

第三四问的代码:

package duixiang.student;

public class studenttext2 {
    public static void main(String[] args) {
        student[] arr = new student[3];
        student st1 = new student(1,"李华",21);
        student st2 = new student(2,"张三",23);
        student st3 = new student(3,"王二",22);
        arr[0]=st1;
        arr[1]=st2;
        arr[2]=st3;
        ///3.通过id删除学生信息
        int index = getIndex(arr, 5);
      if(index>=0){
        arr[index]=null;
        //4.遍历数组
          printArr(arr);
      }else {
          System.out.println("当前id不存在,删除失败");
      }
    }

    //找到id在数组中的索引,获取数组,并需要使用方法的结果
    public static int getIndex(student[]arr,int id){
        for (int i = 0; i < arr.length; i++) {
            //依次获取每个数组的对象
            student stu = arr[i];
            if(stu!=null){
                int stuId = stu.getId();
                if(stuId==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.getName()+" ,"+stu.getAge());
            }

        }
    }
}

运行结果:

第五问的代码:

(问题修改一下,查询id为2的学生,如果存在,将年龄+1岁)

package duixiang.student;

public class studenttext3 {
    public static void main(String[] args) {
        student[] arr = new student[3];
        student st1 = new student(1,"李华",21);
        student st2 = new student(2,"张三",23);
        student st3 = new student(3,"王二",22);
        arr[0]=st1;
        arr[1]=st2;
        arr[2]=st3;
///查询id为2的学生,如果存在,将年龄+1岁
        //需要找id,id索引
        int index=getIndex(arr,2);
        if(index>=0){
            student stu = arr[index];
            int newage=stu.getAge()+1;
            stu.setAge(newage);
            printArr(arr);

        }else {
            System.out.println("当前id不存在,修改失败");
        }
    }
    //找到id在数组中的索引,获取数组,并需要使用方法的结果
    public static int getIndex(student[]arr,int id){
        for (int i = 0; i < arr.length; i++) {
            //依次获取每个数组的对象
            student stu = arr[i];
            if(stu!=null){
                int stuId = stu.getId();
                if(stuId==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.getName()+" ,"+stu.getAge());
            }

        }
    }
}

运行结果:

  • 24
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值