从键盘输入的值 放到javabean 函数数组中
类
package com.fqs.demo1; public class Car { private String pinpai; private Double price; private String color; public Car() { } public Car(String pinpai,Double price,String color) { this.pinpai=pinpai; this.price=price; this.color=color; } //set get pinpai; public void setPinpai(String pinpai) { this.pinpai=pinpai; } public String getPinpai() { return pinpai; } //set get price public void setPrice(Double price) { this.price=price; } public double getPrice() { return price; } //set get color public void setColor(String color) { this.color=color; } public String getColor() { return color; } }
学习后的对象
package com.fqs.demo1; import java.util.Scanner; public class CarTest { public static void main(String[]args) { //1.创建一个数组用来存3个汽车的对象 Car[]arr=new Car[3]; //2.创建数据对象,数据来自于键盘录入 Scanner s=new Scanner(System.in); for (int i=0;i<arr.length;i++) { //---首先,初始化对象 Car c=new Car(); System.out.println("第"+(i+1)+"次:"); //1.1提示输入品牌 System.out.println("品牌:"); String pinpai=s.next(); //1.2放品牌 c.setPinpai(pinpai); //2.1提示输入价格 System.out.println("价格:"); double price=s.nextDouble(); //2.2放价格 c.setPrice(price); //3.1提示输入颜色 System.out.println("颜色:"); String color=s.next(); //3.1提示输入颜色 c.setColor(color); //----最后,将获取的值存到数组中 arr[i]=c; } //3.输出结果 for (int i=0;i<arr.length;i++) { Car c=arr[i]; System.out.println(c.getPinpai()+" "+c.getPrice()+" "+c.getColor()); } } }
自己写的对象
package com.fqs.demo1; import java.util.Scanner; public class CarTest { public static void main(String[]args) { for (int i=0;i<3;i++) { System.out.println("第"+(i+1)+"次:"); Scanner s=new Scanner(System.in); //提示输入品牌 System.out.println("品牌:"); String pinpai1=s.next(); //提示输入价格 System.out.println("价格:"); double price1=s.nextDouble(); //提示输入颜色 System.out.println("颜色:"); String color1=s.next(); Car c=new Car(pinpai1,price1,color1); System.out.println(c.getPinpai()+" "+c.getPrice()+" "+c.getColor()); } } }