对于参数类型String,void,未定义operator + [英] The operator + is undefined for the argument type(s) String, void

查看:358
本文介绍了对于参数类型String,void,未定义operator +的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 具有void返回类型。更改方法的返回类型以返回 int

findAverage has a void return type. Change the return type for the method to return an int value

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

这篇关于对于参数类型String,void,未定义operator +的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆