提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用固定大小的数组
public class Main { /**from 时 代 J a v a**/ private int[] myValues; public void setGrades(int[] grades) { this.myValues = grades; } // Return cloned grades so the caller cannot modify our internal grades public int[] getGrades() { return myValues != null ? myValues.clone() : null; } public int meanGrade() { int mean = 0; if (myValues != null && myValues.length > 0) { int sum = 0; for (int i = 0; i < myValues.length; i++) {// 来自 时 代 J a v a 公 众 号 - N o w J a v a . c o m sum += myValues[i]; } mean = sum / myValues.length; } return mean; } public int minGrade() { int min = 0; for (int index = 0; index < myValues.length; index++) { if (myValues[index] < min) { min = myValues[index]; } } return min; } public int maxGrade() { int max = 0; for (int index = 0; index < myValues.length; index++) { if (myValues[index] > max) { max = myValues[index]; } } return max; } static int[] initGrades() { int[] grades = { 57, 88, 67, 95, 99, 74, 81 }; return grades; } public static void m