java - 未为参数类型定义运算符+字符串,无效

标签 java compiler-errors

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

public static int findAverage(int[] array) {
 ...
 return total / array.length;
}

关于java - 未为参数类型定义运算符+字符串,无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22910467/

相关文章:

c++ - Eclipse : how to enable compile options, build error in C++, OpenCL program

java - JSR 303 Bean Validation 可以与 Spring Data Rest 一起使用吗?

java - 用于查找不包含某些字符的文本的负正则表达式

带有struct参数的c++静态方法

scala - 尝试调试时总是获取ClassNotFoundException

c - 我试图将 return 放在代码末尾,但他们总是在 return 之前用 while 搞乱我

compiler-errors - 设置代码::Blocks to compile MPI programs with MPICC.exe

Java - 对象越少,使用的内存就越大

java - 如何解决java.lang.NoClassDefFoundError : javax/annotation/Generated?

java - 混淆 Java 日历和时区