这篇涉及对象练习以及之前的课程留下的二维数组,随便写写喽………毫无规律

二维数组

来自黑马程序员Java课程p80:https://www.bilibili.com/video/BV17F411T7Ao?p=80&vd_source=94fb2114c3889338d33bcb41c3e25538

package test;

public class itheima_p80 {
    public static void main(String[] args) {
        //定义二维数组
        int arr[][] = {
                {22, 66, 44},
                {77, 33, 88},
                {25, 45, 65},
                {11, 66, 99}
        };
        int yearSum = 0;
        for (int i = 0; i < arr.length; i++) {
            //i: 二维数组中的每个索引
            int newArr[] = arr[i];//newArr:二维数组中的每个元素,即,每个一维数�?
            int sum = getSum(newArr);
            System.out.print("\n�?" + (i + 1) + "个季度的营业额为: " + sum);
            yearSum = yearSum + sum;
        }
        System.out.print("\n全年营业额为: " + yearSum);
    }

    public static int getSum(int arr[]) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum = sum + arr[i];
        }
        return sum;
    }
}

文字格斗游戏

黑马程序员Javap91:https://www.bilibili.com/video/BV17F411T7Ao?p=91&vd_source=94fb2114c3889338d33bcb41c3e25538

主入口文件:RoleTest.java

package test3;
/*
主入口文件RoleTest.java
这里只让用户输入姓名以及性别,对于血量为我们控制�?100滴血
 */
import java.util.Scanner;

public class RoleTest {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一个角色的姓名:");
        String n1 = sc.next();
        System.out.println("请输入第一个角色的性别:");
        String gender1 = sc.next();
        System.out.println("请输入第二个角色的姓�?:");
        String n2 = sc.next();
        System.out.println("请输入第二个角色的性别:");
        String gender2 = sc.next();

        Role r1 = new Role(n1,100,gender1);
        Role r2 = new Role(n2,100,gender2);

        System.out.println("第一个角色信息为:");
        r1.showRole();

        System.out.println("第二个角色信息为:");
        r2.showRole();
        System.out.println();

        System.out.println("游戏开始\n");
        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;
            }
        }
    }


}

JavaBean文件:Role.java

package test3;
/*
Role.java文件
 */
import java.util.Random;

public class Role {
    private String name;
    private int blood;
    private String gender;
    private String face;//这里代表的是长相,长相随机

    //长相即face
    String[] boyFaces = {"风流俊雅", "气宇轩昂", "相貌英俊", "五官端正", "相貌平平", "一塌糊�?", "面目狰狞"};
    String[] girlFaces = {"美奂绝伦", "沉鱼落雁", "婷婷玉立", "身材娇好", "相貌平平", "相貌简�?", "惨不忍睹"};

    //攻击动作
    String[] attacks_desc = {
            "%s使出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去,",
            "%s使出了一招【游空探爪】,飞起身形自半空中变掌为抓锁向%s,",
            "%s大喝一声,身形下伏,一招【劈雷坠地】,捶向%s双腿,",
            "%s运气于掌,一瞬间掌心变得血红,一式【掌心雷】,推向%s,",
            "%s阴手翻起阳手跟进,一招【没遮拦】,结结实实的捶�?%s,",
            "%s上步抢身,招中套招,一招【劈挂连环】,连环攻向%s,"
    };

    //受伤动作
    String[] injured_desc = {
            "结果%s退了半步,毫发无损",
            "结果�?%s造成一处瘀�?",
            "结果一击命中,%s痛得弯下�?",
            "结果%s痛苦地闷哼了一声,显然受了点内�?",
            "结果%s摇摇晃晃,一跤摔倒在�?",
            "结果%s脸色一下变得惨白,连退了好几步",
            "结果『轰』的一声,%s口中鲜血狂喷而出",
            "结果%s一声惨叫,像滩软泥般塌了下�?"
    };

    public Role() {
    }

    public Role(String name, int blood, String gender) {
        this.name = name;
        this.blood = blood;
        this.gender = gender;
        setFace(gender);
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

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

    /**
     * 获取
     *
     * @return blood
     */
    public int getBlood() {
        return blood;
    }

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

    /**
     * 获取
     *
     * @return gender
     */
    public String getGender() {
        return gender;
    }

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

    /**
     * 获取
     *
     * @return face
     */
    public String getFace() {
        return face;
    }

    /**
     * 设置
     *
     * @param gender
     * 对性别进行判断,随机生成长相
     */
    public void setFace(String gender) {
        Random f = new Random();
        if (gender == "�?") {
            int index = f.nextInt(boyFaces.length);
            this.face = boyFaces[index];
        } else if (gender == "�?") {
            int index = f.nextInt(girlFaces.length);
            this.face = girlFaces[index];
        } else {
            this.face = "面目狰狞";
        }
    }

    public void attack(Role role) {//这里role为参�?,代表被攻击的�?
        Random r = new Random();
        int index = r.nextInt(attacks_desc.length);
        String kung_fu = attacks_desc[index];
        System.out.printf(kung_fu, this.getName(), role.getName());
        System.out.println();


        int hurt = r.nextInt(20) + 1;
        int finalBlood = role.blood - hurt;

        finalBlood = finalBlood < 0 ? 0 : finalBlood;

        role.setBlood(finalBlood);

        System.out.println("�?"+role.getName()+"造成�?"+hurt+"点伤�?");

        if (finalBlood > 90) {
            System.out.printf(injured_desc[0], role.getName());
            System.out.println();
            System.out.println(role.getName()+"还剩"+finalBlood+"点血");
        } else if (finalBlood > 80) {
            System.out.printf(injured_desc[1], role.getName());
            System.out.println();
            System.out.println(role.getName()+"还剩"+finalBlood+"点血");
        } else if (finalBlood > 70) {
            System.out.printf(injured_desc[2], role.getName());
            System.out.println();
            System.out.println(role.getName()+"还剩"+finalBlood+"点血");
        } else if (finalBlood > 60) {
            System.out.printf(injured_desc[3], role.getName());
            System.out.println();
            System.out.println(role.getName()+"还剩"+finalBlood+"点血");
        } else if (finalBlood > 40) {
            System.out.printf(injured_desc[4], role.getName());
            System.out.println();
            System.out.println(role.getName()+"还剩"+finalBlood+"点血");
        } else if (finalBlood > 20) {
            System.out.printf(injured_desc[5], role.getName());
            System.out.println();
            System.out.println(role.getName()+"还剩"+finalBlood+"点血");
        } else if (finalBlood > 10 && finalBlood <= 20) {
            System.out.printf(injured_desc[6], role.getName());
            System.out.println();
            System.out.println(role.getName()+"还剩"+finalBlood+"点血");
        } else {
            System.out.printf(injured_desc[7], role.getName());
            System.out.println();
            System.out.println(role.getName()+"还剩"+finalBlood+"点血");
        }
        System.out.println();
    }


    public void showRole() {
        System.out.println("姓名�?:"+getName());
        System.out.println("血量为:"+getBlood());
        System.out.println("性别�?:"+getGender());
        System.out.println("样貌�?:"+getFace());
    }
}

对象数组练习

p92、p93,因为两个很相似,我就直接用一个来进行练习�?

https://www.bilibili.com/video/BV17F411T7Ao/?p=92&vd_source=94fb2114c3889338d33bcb41c3e25538

https://www.bilibili.com/video/BV17F411T7Ao/?p=93&spm_id_from=pageDriver&vd_source=94fb2114c3889338d33bcb41c3e25538

那么这道题就为:

定义数组来储�?3部汽车对象,汽车对象的属性为品牌,价格,颜色

创建三个汽车对象,数据通过键盘录入来获取,并把数据储存到数组,要求�?

1、计算出汽车的平均价�?

2、统计出价格比平均价格的汽车有几辆?并把他们的信息打印出来�?

JavaBean文件

package test4;

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


    public Car() {
    }

    public Car(String brand, int prize, String color) {
        this.brand = brand;
        this.prize = prize;
        this.color = color;
    }

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

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

    /**
     * 获取
     * @return prize
     */
    public int getPrize() {
        return prize;
    }

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

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

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

主文�?

package test4;

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 car = new Car();

            System.out.println("请输入第" + (i + 1) + "个汽车的品牌:");
            String brand = sc.next();
            car.setBrand(brand);
            System.out.println("请输入第" + (i + 1) + "个汽车的价格,以万元为单位:");
            int prize = sc.nextInt();
            car.setPrize(prize);
            System.out.println("请输入第" + (i + 1) + "个汽车的颜色:");
            String color = sc.next();
            car.setColor(color);
            arr[i] = car;
        }
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            Car car = arr[i];
            System.out.println("�?" + (i + 1) + "辆车的信息为");
            System.out.println(car.getBrand() + "; " + car.getPrize() + "万元; " + car.getColor());
            sum = sum + car.getPrize();
        }
        int count = 0;
        System.out.println("车辆平均价格�?: " + (sum / arr.length) + "万元");
        int avg = sum/ arr.length;
        System.out.println("大于平均价格的车辆有:");
        for (int i = 0; i < arr.length; i++) {
            Car car = arr[i];
            if(car.getPrize()>avg){
                System.out.println(car.getBrand() + "; " + car.getPrize() + "万元; " + car.getColor());
                count++;
            }
        }
        System.out.println("高于平均价格的车辆有"+count+"�?");
    }
}