package train2;
public class Phone {
public String brand;
public int price;
public String color;
public Phone() {
}
public Phone(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;
}
}
package train2;
import java.util.Scanner;
public class PhoneTest {
public static void main(String[] args) {
//1.创建数组
Phone[] arr = new Phone[3];
//2.创建对象键盘录入
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
//创建对象
Phone c = new Phone();
//录入品牌
System.out.println("请输入第" + i + "个手机的品牌");
String brand = sc.next();
c.setBrand(brand);
//录入价格
System.out.println("请输入第" + i + "个手机的价格");
int price = sc.nextInt();
c.setPrice(price);
//录入颜色
System.out.println("请输入第" + i + "个手机的颜色");
String color = sc.next();
c.setColor(color);
//把汽车对象添加到数组中
arr[i] = c;
}
//3.遍历打印
for (int i = 0; i < arr.length; i++) {
Phone phone = arr[i];
System.out.println(phone.getBrand() + "," + phone.getPrice() + "," + phone.getColor());
}
//4.获取三部手机的价格
int sum = 0;
for (int j = 0; j < arr.length; j++) {
Phone phone = arr[j];
sum = sum + phone.getPrice();
}
//5.求平均值
//数据尽量以字母表示
float avg = sum / arr.length;
System.out.println("平均价格为" + avg);
}
}