public static void main(String[] args) {
// 声明数组
int[] prices = new int[5];
int maxPrice = 0, avgPrice = 0, sumPrice = 0;
Scanner input = new Scanner(System.in);
System.out.println("请输入5件商品的价格:");
for (int i = 0; i < 5; i++) {
prices[i] = input.nextInt(); // 循环向数组中元素赋值
}
// 计算价格最大值
maxPrice = prices[0]; // 假设最大值为第一个元素
for (int index = 1; index < prices.length; index++) {
sumPrice += prices[index]; // 汇总价格
if (prices[index] > maxPrice) {
maxPrice = prices[index];
}
}
// 平均价格=总价格/商品数量
avgPrice = sumPrice / prices.length;
System.out.println("本货架上商品的总价格为:" + sumPrice + " 平均价格为:" + avgPrice + " 最高价格为:" + maxPrice);
}
该程序运行后的结果如下所示。
请输入5件商品的价格: 88 64 44 62 79 本货架上商品的总价格为:249 平均价格为:49 最高价格为:88
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有