Java基础 --- 面向对象综合训练

面向对象综合训练


案例一:文字版格斗游戏

格斗游戏,每个游戏角色的姓名,血量,都不相同,在选定人物的时候(new对象的时候),这些信息就应该被确定下来。

补充:

public class Test {
    public static void main(String[] args) {
        //两部分参数
        //第一部分参数:要输出的内容%s(占位)
        //第二部分参数:填充的数据
        System.out.printf("你好啊%s","张三");
        System.out.println();//printf无法换行,这里做换行处理
        System.out.printf("%s你好啊%s","张三","李四");
    }
}

正题:

import java.util.Random;

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双腿。",
            "%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 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();
        //计算造成的伤害1 ~ 20
        int hurt = r.nextInt(1,21);
        //修改一下挨揍的人的血量
        //剩余血量
        int remainBlood = role.getBlood() - hurt;
        //对剩余血量做一个验证,如果为负数了,就修改为0
        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());
    }
}

public class GameTest {
    public static void main(String[] args) {
        //1.创建第一个角色
        Role r1 = new Role("炎拳", 100,'男');
        //2.创建第二个角色
        Role r2 = new Role("巳蛇",100,'女');
        
        //展示一下角色的信息
        r1.showRoleInfo();
        r2.showRoleInfo();
        //3.开始格斗
        while(true){
            //r1开始攻击r2
            r1.attack(r2);
            //判断r2的剩余血量
            if(r2.getBlood() == 0){
                System.out.println(r1.getName() + "K.O了" + r2.getName());
                break;
            }
            //r2攻击r1
            r2.attack(r1);
            if(r1.getBlood() == 0){
                System.out.println(r2.getName() + "K.O了" + r1.getName());
                break;
            }
        }
    }
}

案例二:对象数组练习


对象数组1

定义数组存储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;
    }
}

class GoodsTest {
    public static void main(String[] args) {
        //1.创建数组
        Goods[] arr = new Goods[3];

        //2.创建三个商品对象
        Goods g1 = new Goods("114514","华为P60",6999.9,100);
        Goods g2 = new Goods("10086","烧水壶",248.0,58);
        Goods g3 = new Goods("404","帕鲁",555.5,1234);

        //3.把商品添加到数组里
        arr[0] = g1;
        arr[1] = g2;
        arr[2] = g3;

        //4.遍历
        for (int i = 0; i < arr.length; i++) {
            Goods goods = arr[i];
            System.out.println(goods.getId() + "," + goods.getName() + "," + goods.getPrice() + "," + goods.getCount());
        }
    }
}

对象数组2

定义数组循序3部汽车对象。

汽车的属性:品牌,价格,颜色。

创建三个汽车对象,数据通过键盘录入而来,并把数据存入到数组当中。

补充:

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        //键盘录入
        //第一套体系:
        //nextInt();接收整数
        //nextDouble();接收小数
        //next();接收字符串
        //遇到空格,制表符,回车就停止接收,这些符号后面的数据就不会接收了

        //第二套体系:
        //nextLine();接收字符串
        //可以接受空格,制表符,遇到回车才停止接收数据
        
        //键盘录入的两套体系不能混用
        //弊端:先用nextInt,再用nextLine会导致下面的nextLine接收不到数据

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数");
        int num1 = sc.nextInt();//123 1
        System.out.println(num1);//123
        System.out.println("请输入第二个整数");
        int num2 = sc.nextInt();
        System.out.println(num2);//1

        System.out.println("请输入一个字符串");
        String str1 = sc.next();//abc a
        System.out.println(str1);//abc
        System.out.println("请输入第二个字符串");
        String str2 = sc.next();
        System.out.println(str2);//a

        System.out.println("请输入一个字符串");
        String line1 = sc.nextLine();
        System.out.println(line1);
        System.out.println("请输入二个字符串");
        String line2 = sc.nextLine();
        System.out.println(line2);
    }
}

正题:

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

    public Car(){
    }

    public Car(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;
    }
}

import java.util.Scanner;

public class CarTest {
    public static void main(String[] args) {
        Car[] arr = new Car[3];//创建数组

        Scanner sc = new Scanner(System.in);//键盘录入
        for (int i = 0; i < arr.length; i++) {
            Car c = new Car();//创建汽车对象
            System.out.println("请输入汽车的品牌");
            String brand = sc.next();//录入品牌
            c.setBrand(brand);
            System.out.println("请输入汽车的价格");
            int price = sc.nextInt();//录入价格
            c.setPrice(price);
            System.out.println("请输入汽车的颜色");
            String color = sc.next();//录入颜色
            c.setColor(color);
            arr[i] = c;//把汽车对象添加到数组当中
        }
        for (int i = 0; i < arr.length; i++) {
            Car car = arr[i];
            System.out.println(car.getBrand() + "," + car.getPrice() + "," + car.getColor());
        }
    }
}

对象数组3

定义数组存储3部手机对象。

手机的属性:品牌,价格,颜色。

要求,计算出三部手机的平均价格。

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

public class PhoneTest {
    public static void main(String[] args) {
        //创建一个数组
        Phone[] arr = new Phone[3];
        //创建手机的对象
        Phone p1 = new Phone("鸭梨",2999,"白色");
        Phone p2 = new Phone("菠萝",3100,"黄色");
        Phone p3 = new Phone("大哥大",1000,"红色");
        //把手机对象添加到数组当中
        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();
        }
        //求平均值
        int avg = sum / arr.length;
        System.out.println(avg);
    }
}

对象数组4

定义数组存储4个女朋友的对象

女朋友的属性:姓名、年龄、性别、爱好

要求1:计算出四女朋友的平均年龄

要求2:统计年龄比平均值低的女朋友有几个?并把她们的所有信息打印出来。

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

public class GirlFriendTest {
    public static void main(String[] args) {
        GirlFriend[] arr = new GirlFriend[4];
        GirlFriend g1 = new GirlFriend("柳如烟",18,"女","逛街");
        GirlFriend g2 = new GirlFriend("沈江雪",25,"女","医学");
        GirlFriend g3 = new GirlFriend("慕容雪",21,"女","书法");
        GirlFriend g4 = new GirlFriend("唐嫣",17,"女","绘画");
        arr[0] = g1;
        arr[1] = g2;
        arr[2] = g3;
        arr[3] = g4;
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            GirlFriend girlFriend = arr[i];
            sum += girlFriend.getAge();
        }
        int avg = sum / arr.length;
        System.out.println(avg);
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            GirlFriend girlFriend = arr[i];
            if(girlFriend.getAge() < avg){
                count++;
                System.out.println(girlFriend.getName() + "," + girlFriend.getAge() + "," + girlFriend.getGender() + "," + girlFriend.GetHobby());
            }
        }
        System.out.println(count);
    }
}

对象数组5

定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同。

学生的属性:学号,姓名,年龄。

要求1:再次添加一一个学生对象,并在添加的时候进行学号的唯一性判断。

要求2:添加完毕之后,遍历所有学生信息。

要求3:通过id删除学生信息

​ 如果存在,则删除,如果不存在,则提示删除失败。

要求4:删除完毕之后,遍历所有学生信息。

要求5:查询数组id为"2” 的学生,如果存在,则将他的年龄+1岁


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

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

要求1~要求2:

public class Test {
    public static void main(String[] args) {
        //创建数组,存储学生对象
        Student[] arr = new Student[3];
        //创建学生对象,添加到数组当中
        Student stu1 = new Student(1,"张三",18);
        Student stu2 = new Student(2,"李四",23);
        Student stu3 = new Student(3,"王五",20);
        //把学生对象添加到数组当中
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
        Student stu4 = new Student(4,"赵六",24);
        //唯一性判断
        //已存在 --- 不用添加
        //不存在 --- 添加
        boolean flag = contains(arr,stu4.getId());
        if(flag){
            //id存在
            System.out.println("当前id重复,请修改id后再重新添加");
        }else {
            //id不存在
            //把stu4添加到数组当中
            //1.数组已经存满 --- 创建一个新的数组,新数组的长度 = 老数组的长度 + 1
            //2.数组没有存满 --- 直接添加
            int count = getCount(arr);
            if(count == arr.length){
                //存满
                //创建一个新的数组,新数组的长度 = 老数组的长度 + 1
                Student[] newArr = creatNewArr(arr);
                newArr[count] = stu4;
                printArr(newArr);
            }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.getId() + "," + stu.getName() + "," + stu.getAge());
            }
        }
    }

    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 contains(Student[] arr,int id){//定义方法,判断id是否存在
        for (int i = 0; i < arr.length; i++) {
            //获取数组中每一个学生对象
            Student stu = arr[i];
            if(stu != null){
                //获取数组中学生对象的id
                int sid = stu.getId();
                if(sid == id){
                    return true;
                }
            }
        }
        return false;
    }
}

要求3~要求4:

public class Test1 {
    public static void main(String[] args) {
        //创建数组,存储学生对象
        Student[] arr = new Student[3];
        //创建学生对象,添加到数组当中
        Student stu1 = new Student(1, "张三", 18);
        Student stu2 = new Student(2, "李四", 23);
        Student stu3 = new Student(3, "王五", 20);
        //把学生对象添加到数组当中
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
        int index = getIndex(arr,2);
        if(index >= 0){
            //存在,删除
            arr[index] = null;
            printArr(arr);
        }else {
            //不存在,删除失败
            System.out.println("当前id不存在,删除失败!");
        }
    }

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

    public static int getIndex(Student[] arr,int id){//找到id在数组中的索引
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                int sid = stu.getId();
                if(sid == id){
                    return i;
                }
            }
        }
        return -1;
    }
}

要求5:

public class Test2 {
    public static void main(String[] args) {
        //创建数组,存储学生对象
        Student[] arr = new Student[3];
        //创建学生对象,添加到数组当中
        Student stu1 = new Student(1, "张三", 18);
        Student stu2 = new Student(2, "李四", 23);
        Student stu3 = new Student(3, "王五", 20);
        //把学生对象添加到数组当中
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
        int index = getIndex(arr,2);
        if(index >= 0){
            //存在,年龄+1
            Student stu = arr[index];
            int newAge = stu.getAge() + 1;
            stu.setAge(newAge);
            printArr(arr);
        }else {
            //不存在,直接提示
            System.out.println("当前id不存在,修改失败!");
        }
    }

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

    public static int getIndex(Student[] arr,int id){//找到id在数组中的索引
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                int sid = stu.getId();
                if(sid == id){
                    return i;
                }
            }
        }
        return -1;
    }
}

热门相关:惹火小辣妻:老公,用力点   锦绣田园:医女嫁贤夫   甜蜜婚令:首长的影后娇妻   大文豪   世界第一校长