public class Statistics { public float average(int... nums){ int sum = 0; for (int x : nums){ sum += x; } return ((float)sum / nums.length); } }
使用方式:
public class Demo { public static void main(String[] args){ Statistics abc = new Statistics(); int[] nums = {32,67,98,34,76}; System.out.println("Average: " + abc.average(nums)); } }