(一)商品数组练习
一、题目:
定义数组存储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());
}
}
}