找出数组中数字的平均值,找到大于平均值的数字

时间:2017-01-26 03:20:38

标签: java arrays if-statement for-loop

我正在尝试使用用户输入将数字放入数组中,然后找到这些数字的平均值,并找出哪些数字大于平均值。数字进入一个数组,但是当我试图找到平均值时,我无法找到平均数,哪些数字大于平均值,因为并非所有变量都可见于试图找到大于平均值。但是,当我允许该部分查看所有变量时(不要在某些部分周围{}),它会找到每个单独数字的平均值。所以现在它打印每个数字的平均值,并且对于大于平均值的数字打印null。如何打印所有10个数字的平均值以及哪个数字大于平均数?

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]);
}

}
}

3 个答案:

答案 0 :(得分:0)

使用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()));
}

答案 1 :(得分:0)

你的第一个for循环试图在找到numbers之和之前处理平均值。此外,请确保注意变量的范围和类型,以确保在需要的地方看到值,并返回正确的结果,例如双类型平均值。如果你看一下你的平均语法,你会发现两个值,即sum和10都是整数,而在Java和其他强类型语言中,整数是从两个整数的除法返回的。你的精确度会丢失,如果它是&lt; 1,将返回0 - 你不会想要那个。

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]);
}

}
}

答案 2 :(得分:0)

由于您不熟悉Java,并且很可能在这里学习入门课程,这是一个使用for循环的答案,如您自己的尝试:

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]);
      }
    }
  }
}

<强>输出:

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

试试here!