需求1:
代码呈现
商品类型
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;
}
/**
* 获取
* @return id
*/
public String getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return price
*/
public double getPrice() {
return price;
}
/**
* 设置
* @param price
*/
public void setPrice(double price) {
this.price = price;
}
/**
* 获取
* @return count
*/
public int getCount() {
return count;
}
/**
* 设置
* @param count
*/
public void setCount(int count) {
this.count = count;
}
public String toString() {
return "Goods{id = " + id + ", name = " + name + ", price = " + price + ", count = " + count + "}";
}
}
测试类
public class GoodsTest {
public static void main(String[] args) {
//创建一个长度为3的数组
Goods [] arr = new Goods[3];
//创建三个商品对象
//商品类型按快捷键 Ctrl + p
Goods g1 = new Goods("001","华为mate70pro",8999.0,100);
Goods g2 = new Goods("002","保温杯",299.0,50);
Goods g3 = new Goods("003","枸杞",12.7,70);
//把商品添加到数组中
arr[0] = g1;
arr[1] = g2;
arr[2] = g3;
//遍历数组
for (int i = 0; i < arr.length; i++) {
Goods goods = arr[i];
System.out.println(goods.getId() + ", "+goods.getName() + ", "+goods.getPrice() + ", " + goods.getCount());
}
}
}
运行结果
需求2
知识点: 键盘录入
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//键盘录入
//第一套体系
//nextInt();接受整数
//nextDouble();接受小数
//next();接受字符串
//遇到空格 制表符 回车就停止接受
//第二套体系
//nextLine();接受字符串
//可以接受空格制表符 回车结束
/*Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数");
int num1 = sc.nextInt();
System.out.println(num1);
System.out.println("请输入第二个整数");
int num2 = sc.nextInt();
System.out.println(num2);
*/
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个字符串");
String line1 = sc.nextLine();
System.out.println(line1);
System.out.println("请输入第二个字符串");
String line2 = sc.nextLine();
System.out.println(line2);
}
}
package Test;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
//键盘录入两套体系不能混用
//弊端:先用nextInt 再用nextLine 会导致nextLine接受不到数据
Scanner sc = new Scanner(System.in);
System.out.println("输入一个整数");
int number = sc.nextInt();
System.out.println(number);
System.out.println("请输入一个字符串");
String line = sc.nextLine();
System.out.println(line);
}
}
代码呈现
package Car;
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;
}
/**
* 获取
* @return brand
*/
public String getBrand() {
return brand;
}
/**
* 设置
* @param brand
*/
public void setBrand(String brand) {
this.brand = brand;
}
/**
* 获取
* @return price
*/
public int getPrice() {
return price;
}
/**
* 设置
* @param price
*/
public void setPrice(int price) {
this.price = price;
}
/**
* 获取
* @return color
*/
public String getColor() {
return color;
}
/**
* 设置
* @param color
*/
public void setColor(String color) {
this.color = color;
}
public String toString() {
return "Car{brand = " + brand + ", price = " + price + ", color = " + color + "}";
}
}
测试类
package Car;
import java.util.Scanner;
public class carTest {
public static void main(String[] args) {
//创建一个数组来存3个汽车对象
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() );
}
}
}
运行结果