Java_题目_对象数组练习(商品与汽车)

(一)商品数组练习
一、题目:
定义数组存储3个商品对象。
商品的属性:商品的id,名字,价格,库存。
创建三个商品对象,并把商品对象存入到数组当中。

二、重难点:
创建bean要熟练。alt+insert。
数组元素是对象。
写构造器时可以用ctrl+p查看对照。

三、代码:
1)商品bean

public class Goods {
    /*
     * 定义数组存储3个商品对象。
     * 商品的属性:商品的id,名字,价格,库存。
     * 创建三个商品对象,并把商品对象存入到数组当中。
     * */
    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;
    }
}

2)商品数组

public class GoodsTest {
    public static void main(String[] args) {
        //1.创建一个数组
        Goods[] arr = new Goods[3];
        //2.创建三个商品对象
        Goods g1 = new Goods("001", "洗衣机", 10000.0, 500);
        Goods g2 = new Goods("002", "洗衣篮子", 50.0, 100);
        Goods g3 = new Goods("003", "洗衣机清洗剂", 10.0, 90);
        //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());
        }
    }
}

(二)汽车数组练习
一、题目:
定义数组存储3部汽车对象。
汽车的属性:品牌,价格,颜色。
创建三个汽车对象,数据通过键盘录入而来,并把数据存入到数组当中。

二、重难点:
1)键盘录入两套体系不能混用,例如先用nextInt再用nextLine会导致nextLine接收不到数据。
第一套体系:遇到空格、制表符、回车就停止接收。
nextInt();接收整数
nextDouble();接收小数
next();接收字符串
第二套体系:
nextLine();接收字符串
2)创建汽车的对象,不可以写到循环外。

三、代码:
1)汽车bean

public class Car {
    /*
     * 定义数组存储3部汽车对象。
     * 汽车的属性:品牌,价格,颜色。
     * 创建三个汽车对象,数据通过键盘录入而来,并把数据存入到数组当中。
     * */

    /*
     * 键盘录入两套体系不能混用,例如先用nextInt再用nextLine会导致nextLine接收不到数据。
     * 第一套体系:遇到空格、制表符、回车就停止接收。
     * nextInt();接收整数
     * nextDouble();接收小数
     * next();接收字符串
     * 第二套体系:
     * nextLine();接收字符串
     * */

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

2)汽车数组:

import java.util.Scanner;

public class CarTest {
    public static void main(String[] args) {
        //1.创建一个数组用来存3个汽车对象
        Car[] arr = new Car[3];
        //2.创建汽车对象,数据来自键盘录入
        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;
        }
        //3.遍历数组
        for (int i = 0; i < arr.length; i++) {
            Car car = arr[i];
            System.out.println(car.getBrand() + "," + car.getPrice() + "," + car.getColor());
        }
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值