一、案例需求

package com.lkbhua.Test3;

public class Goods {
    private String id;
    private String name;
    private double price;
    private int count;

    // 标准的JavaBean
    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 com.lkbhua.Test3;

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

        // 2、创建三个商品对象
        Goods g1 = new Goods("001", "iphone", 8000, 100);
        Goods g2 = new Goods("002", "MacBook", 12000, 50);
        Goods g3 = new Goods("003", "iPad", 3000, 200);

        // 3、将商品对象,保存到数组中
        goods[0] = g1;
        goods[1] = g2;
        goods[2] = g3;

        // 4、遍历数组,并输出商品信息
        for (int i = 0; i < goods.length; i++) {
            Goods g = goods[i];
            System.out.println(g.getId() + " " + g.getName() + " " + g.getPrice() + " " + g.getCount());
        }
    }
}

二、增加需求

如果创建汽车对象的代码不能写在循环外面,一定要写在里面!

每一次循环录入的数据都会覆盖上一次循环录入的数据,直至最后一次循环录入,而因为创建对象的代码在循环外面,每一次循环遍历数组内的索引均指向同一个对象,那么会造成如图所示情况。

正确代码:应该放在循环里面,每一次录入之前创建一个新的对象,来接受录入的数据,并且更改数组索引。

package com.lkbhua.Test4;

import java.awt.*;

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 void setBrand(String brand) {
        this.brand = brand;
    }
    public String getBrand() {
        return brand;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public int getPrice() {
        return price;
    }
    public void setColor(String Color) {
        this.Color = Color;
    }
    public String getColor() {
        return Color;
    }

}
package com.lkbhua.Test4;

import java.util.Scanner;

public class CarTest {
    public static void main(String[] args) {
        // 1、创建一个数组
        Car[] cars = new Car[3];

        // 2、创建对象,数据来自键盘录入
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < cars.length; i++) {
            // 创建汽车对象
            Car car = new Car();

            // 录入品牌
            System.out.println("请输入汽车品牌:");
            String brand = sc.next();
            car.setBrand(brand);
            // 录入价格
            System.out.println("请输入汽车价格:");
            int price = sc.nextInt();
            car.setPrice(price);
            // 录入颜色
            System.out.println("请输入汽车颜色:");
            String color = sc.next();
            car.setColor(color);

            // 添加到数组
            cars[i] = car;
            System.out.println("录入完毕,请继续录入下一个汽车信息:");
            System.out.println("------------------");
        }

        // 3、遍历数组,输出汽车信息
        for (int i = 0; i < cars.length; i++) {
            Car car = cars[i];
            System.out.println("汽车品牌:" + car.getBrand() + "汽车价格:" + car.getPrice() + "汽车颜色:" + car.getColor());
        }
    }
}

三、关于键盘录入的注意事项

声明:

以上均来源于B站@ITheima的教学内容!!!

本人跟着视频内容学习,整理知识引用

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐