92 从键盘获取3个汽车 汽车包含品牌 价格 颜色
对象
package com.fqs.Car; public class Car { //汽车的三个属性 private String brand; private double price; private String color; //javabean 格式 public Car() { } public Car(String brand, double 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 double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
调用方法
package com.fqs.Car; import java.util.Scanner; public class CarTest { public static void main(String[] args) { //存放3个汽车的数组 Car[]carArr=new Car[3]; Scanner s=new Scanner(System.in); for (int i = 0; i <carArr.length ; i++) { //创建汽车对象 Car c=new Car(); System.out.println("请输入车辆的品牌"); String brand=s.next(); //将键盘获取的值传给车的品牌 c.setBrand(brand); System.out.println("请输入车辆的价格"); double price=s.nextDouble(); c.setPrice(price); System.out.println("请输入车辆的颜色"); String color=s.next(); c.setColor(color); //将c 和arrCar关联起来 carArr[i]=c; } for (int i = 0; i <carArr.length ; i++) { System.out.println(carArr[i].getBrand()+carArr[i].getPrice()+carArr[i].getColor()); } } }