public class chap7p4 { public static void main(String[] args) { int[] heights = { 33, 45, 23, 43, 48, 32, 35, 46, 48, 39, 41, }; printArray(heights); System.out.println("Average is " + findAverage(heights)); // this is where I get the error } public static void printArray(int[] array) { for (int eachNum : array) { System.out.println(eachNum + " "); } } public static void findAverage(int[] array) { int average = 0; int total = 0; for (int i = 0; i <= array.length; i++) { total = total + array[i]; } average = total / array.length; System.out.println(average); } }
我得到这个错误
"Exception in thread "main" java.lang.Error: Unresolved compilation problem: The operator + is undefined for the argument type(s) String, void"
findAverage具有无效的返回类型。更改方法的返回类型以返回int值
findAverage
int
public static int findAverage(int[] array) { ... return total / array.length; }