//创建一个类 public class phone { //定义数组存储3部手机对象 //手机的属性:品牌,价格,颜色 //要求:计算出三部手机的平均价格 String brond; double price; String color; public phone() { } public phone(String brond, double price, String color) { this.brond = brond; this.price = price; this.color = color; } public String getBrond() { return brond; } public void setBrond(String brond) { this.brond = brond; } 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 test4; import java.util.Scanner; public class phoneTest { public static void main(String[] args) { phone [] arr=new phone[3]; phone p1=new phone("小米",1999,"白色"); phone p2=new phone("华为",2999,"蓝色"); phone p3=new phone("苹果",3999,"暗夜紫"); arr[0]=p1; arr[1]=p2; arr[2]=p3; //获取三部手机的平均价格 double sum = 0; for (int i = 0; i < arr.length; i++) { phone phone=arr[i]; sum+=phone.getPrice(); } double avg=sum/arr.length; System.out.printf("平均价格为%.2f",avg); } }