even and odd averages using array

2019-02-28 10:58发布

Write a program that reads a list of 10 values from the user. Put the values in an array. The program should read the array and then calculate and display the average of the even input values and the average of the odd input values. This should be done using objects, methods, and a tester class.

I cannot figure out why I am receiving the error:

bad operand types for binary operator.

I do not know what to change. I know something is wrong with my mod (%).

Here is what I have so far for my Average class:

public class Average 
{
    private int[] numbers = new int[10];
    double aveEven, aveOdd,sumEven=0,sumOdd=0; 
    int oddCounter=0, evenCounter=0; 
    public Average(int[] n)
    {
        numbers = n;

        if (numbers % 2 == 0)/something is wrong here/
        { 
            evenCounter++; 
            sumEven+=n; 

        } 
        else
        { 
            oddCounter++; 
            sumOdd+=n; 

        } 
    }

    public void aveEven()
    {
        for (int i = 0; i < numbers.length; i++)
        {
            aveEven = sumEven/evenCounter;
            System.out.println("The even average is: " + aveEven);
        }
    }

    public void aveOdd()
    {
        for(int i = l; i < numbers.length;  i++)
        {
            aveOdd = sumOdd/oddCounter;
            System.out.println("The odd average is: " + aveOdd);
        }
    }
}

For the AverageTester class I have the following:

import java.util.Scanner;
public class AverageTester 
{public static void main(String[] args) 
     {
        int[] integer = new int[10];

        Scanner input = new Scanner(System.in);

        for(int i=0 ; i < 10 ; i++)
        {
            System.out.print("Please enter a number : ");
            integer[i] = input.nextInt();
        }

        Average example = new Average(integer);
        example.aveOdd();


    }
}

Also, If you see anything else that could be wrong, please let me know. Thank you.

3条回答
Luminary・发光体
2楼-- · 2019-02-28 11:29

numbers is an array, so numbers % 2 is invalid. You should loop over the array and use the % operator on the elements of the array. The += operator should also be applied on a element of the array (i.e. numbers[i]) and not the entire array.

numbers = n;
for (int i = 0; i < numbers.length; i++) {
    if (numbers[i] % 2 == 0) { 
        evenCounter++; 
        sumEven+=numbers[i]; 
    }  else { 
        oddCounter++; 
        sumOdd+=numbers[i]; 
    } 
}

As for aveEven and aveOdd, since you already compute the sums in the constructor (or at least it seems like that's what you intended to do), you don't need a loop in these methods.

EDIT :

I originally assumed you intended to calculate the average of the numbers in even positions in the array and the average of the numbers in odd positions. After reading the question again, I think the odd/even refers to the numbers themselves, so I changed the code accordingly.

查看更多
虎瘦雄心在
3楼-- · 2019-02-28 11:36

Numbers is an array and comparing an array to an int doesn't work, you could do something like this (depending on your logic):

    for(int number : numbers){
        if(number % 2 == 0){
            evenCounter++;
            sumEven += n;

        }else{
            oddCounter++;
            sumOdd += n;

        }
    }
查看更多
放我归山
4楼-- · 2019-02-28 11:40

Few Mistakes

1.) if (numbers % 2 == 0), numbers is an array, use index here and loop. like this if (numbers[i] % 2 == 0).

2.) sumEven += n;, again n is an array here, need to use index.

3.) for (int i = l ; i < numbers.length ; i++) {, you have used l instead of 1.

查看更多
登录 后发表回答