错误的代码 + 幸运的数据 = 正确的结果

正确逻辑 if (price > maxPrice) { maxPrice = price; } if (price < minPrice) { minPrice = price; }

/**
复制代码
* 定义3种装修套餐价格数组{899,1299,1999},输出最低价、最高价、平均价 * <p> * 注意: * 1、for循环 * 2、常量 * 3、数组 * */ public class Day15RenovationPackagePrice { public static final double[] RENOVATION_PACKAGE_PRICE = {6799, 2399, 4399, 1999, 899, 3299, 1299}; public static void main(String[] args) { double minPrice = RENOVATION_PACKAGE_PRICE[0]; double maxPrice = RENOVATION_PACKAGE_PRICE[0]; double sum = 0; int length = RENOVATION_PACKAGE_PRICE.length; for (double price : RENOVATION_PACKAGE_PRICE) { sum += price; // 总金额 if (price < minPrice) { minPrice = price; // 最小值 } if (price > maxPrice) { maxPrice = price; // 最大值 } } System.out.println("--------------------装修套餐价格--------------------"); System.out.printf("装修套餐的最低价是:%,8.2f 元/m² %n", minPrice); System.out.printf("装修套餐的最高价是:%,8.2f 元/m² %n", maxPrice); System.out.printf("装修套餐的平均价是:%,8.2f 元/m² %n", sum / length); } }
0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
下载 APP