运行 ❯
获取您
自己的 Java
服务器
×
更改方向
更改主题,暗色/亮色
转到 Spaces
public class Main { public static void main(String[] args) { // An array storing different ages int ages[] = {20, 22, 18, 35, 48, 26, 87, 70}; float avg, sum = 0; // Get the length of the array int length = ages.length; // Loop through the elements of the array for (int age : ages) { sum += age; } // Calculate the average by dividing the sum by the length avg = sum / length; // Print the average System.out.println("The average age is: " + avg); } }
平均年龄为:40.75