The operator + is undefined for the argument type(s) String, void

后端 未结 6 385
执念已碎
执念已碎 2021-01-29 13:41
public class chap7p4 {
    public static void main(String[] args) {
        int[] heights = { 33, 45, 23, 43, 48, 32, 35, 46, 48, 39, 41, };
        printArray(heights);         


        
相关标签:
6条回答
  • 2021-01-29 13:46

    you cannot make

    String + void

    findAverage method returns void

    0 讨论(0)
  • 2021-01-29 13:48

    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;
    }
    
    0 讨论(0)
  • 2021-01-29 13:51

    Also here argument is of type int, and Operators like(*,+,..) won't work for argument type void and int so either change argument type or return type as mentioned above.

    0 讨论(0)
  • 2021-01-29 13:56

    Your method findAverage(heights) has to return a value to be applicaable for the binary operator +, which takes two operants.

    0 讨论(0)
  • 2021-01-29 14:06

    Return type of findAverage method should not be void it should be integer for your code. You should not print the value of average in same method as you are calling in main method.

    0 讨论(0)
  • 2021-01-29 14:09

    Change return type of your findAverage() method,

    i.e void findAverage to int findAverage

    public static int findAverage(int[] array) {
        int total = 0;
        for (int i = 0; i <= array.length; i++) {
            total = total + array[i];
        }
        return total / array.length;
    }
    
    0 讨论(0)
提交回复
热议问题