简体   繁体   中英

Find the average of numbers in an array and find the numbers greater than the average

I am trying to put numbers into an array using user input then find the average of those number, and also find which numbers are greater than the average. The numbers are going into an array but when I am trying to find the average I am not able to find the average and which numbers are greater than the average because not all of the variables are visible to the part trying to find the numbers greater than the average. But, when I allow that part to see all of the variables (don't but {} around some parts) it finds the average for each individual number. So right now it prints the average for each number, and it prints null for the numbers greater than the average. How do I get it to print the average for all 10 numbers and which numbers are greater than the average?

import java.util.Scanner;
public class MeanAndHigher
{
public static void main(String [] args)
{
Scanner reader = new Scanner(System.in);
Number[]numbers = new Number[10];
Number[] greaterList = new Number[10];
int sum = 0;
int greaterListIndex = 0;
for (int i = 0; i < numbers.length; i++) {
    System.out.println("Enter a number: ");
    int a = reader.nextInt();
    sum += a ;
    {
        double average= sum/10;
        if (a > average)
            greaterList[greaterListIndex]=numbers[i];
        greaterListIndex++;
        System.out.println("The average is: " + average);
    }
}

System.out.println("Numbers greater than the average are: "); 
for( int i = 0; i < greaterListIndex; i++) {
    System.out.println(greaterList[i]);
}

}
}

You can do something as simple as this with Java 8:

public static void main(String[] args) {
    int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    double average = Arrays.stream(array).average().getAsDouble();

    System.out.println("Average: " + average);

    System.out.println("Numbers Above Average: " + Arrays.stream(array).filter(i -> i > average).boxed().collect(Collectors.toList()));
}

Your first for loop is trying to handle the averaging before the sum of numbers has been found. Also, make sure you pay attention to scope and type of variables to ensure that values are seen where needed, and that the proper results, such as a double-type average is returned. If you look at your averaging syntax, you'll see that both values, that is, sum and 10 are integers, and in Java, and other strongly-typed languages, an integer is returned from a division of two integers. Your precision would be lost, and if it were < 1, a 0 would be returned - you wouldn't want that.

import java.util.Scanner;
public class MeanAndHigher
{
public static void main(String [] args)
{
Scanner reader = new Scanner(System.in);
Number[]numbers = new Number[10];
Number[] greaterList = new Number[10];
int sum = 0;
int greaterListIndex = 0;
int a;
double average;
for (int i = 0; i < numbers.length; i++) {
System.out.println("Enter a number: ");
 a = reader.nextInt();
 sum += a ;
}
    average = sum/(double)numbers.length;

for(int j = 0; j < numbers.length; j++){
    if (numbers[j] > average)
        greaterList[greaterListIndex] = numbers[j];
    greaterListIndex++;
}
    System.out.println("The average is: " + average);

System.out.println("Numbers greater than the average are: "); 
for( int k = 0; k < greaterListIndex; k++) {
System.out.println(greaterList[k]);
}

}
}

Since you are new to Java and most likely taking an introductory course here is an answer that uses for-loops like your own attempt:

import java.util.Arrays;
import java.util.Scanner;

public class MeanAndHigher {
  public static void main(String [] args) {
    Scanner s = new Scanner(System.in);

    System.out.print("How many numbers will be in your array? ");
    int n = s.nextInt();

    int arr[] = new int[n];
    int sum = 0;

    System.out.println("Enter the elements of the array:");
    for(int i = 0; i < n; i++) {
      arr[i] = s.nextInt();
      sum = sum + arr[i];
    }

    System.out.println("You entered the array: " + Arrays.toString(arr));

    double average = sum / arr.length;
    System.out.println("The average of the array is: " + average);

    System.out.println("The numbers in the array that are greater than the average are: ");
    for(int i = 0; i < arr.length; i++) {
      if(arr[i] > average) {
        System.out.println(arr[i]);
      }
    }
  }
}

Output:

How many numbers will be in your array? 3
Enter the elements of the array:
5
3
7
You entered the array: [5, 3, 7]
The average of the array is: 5.0
The numbers in the array that are greater than the average are: 
7

Try it here!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM