mercredi 9 novembre 2016

Finding numbers in an array above the average number

I am trying to figure out why my code won't allow me to correctly print any values in the array that are above the average value. I looked at a stackoverflow example of another user with a similar issue but it still didn't make sense. I don't even understand the current output for the values above the average.

How do I print the values in the array that are above the average value of the calculated array?

I'd appreciate the help.

My code:

public class Inequality 
{
    public static void main(String[] args) 
    {
        // list of incomes in thousands
         int[] income = {2,10, 532, 4, 53, 28, 291, 38, 6, 17, 73, 21};
         int sum = 0;
         int average;
         int aboveAverage = 0;

         for (int i = 0;i< income.length;i++)
         {
             sum = sum + income[i];
         }
         average = sum/income.length;
         System.out.println("Average of income array: " + average);

         for (int i = 0; i < income.length; i++)
         {
             if (income[i]>average)
             {
                 aboveAverage++;
             }
         }
         System.out.println("There are " + aboveAverage + " numbers above the average.");
         System.out.println("Range of numbers above the average: " + income);
    }
}

My output:

Average of income array: 89
There are 2 numbers above the average.
Range of numbers above the average: [I@15db9742

Aucun commentaire:

Enregistrer un commentaire