尝试获取二维数组中每个元素的平均值

时间:2019-03-19 09:31:25

标签: java text-files

我有一个程序,我必须从文本文件中找到每个学生分数的平均值,并将其显示为2D数组。

我被困住了-我每次要做的就是获取每一行或每一列的平均值,但是我必须在文本文件中找到每个值的平均值。

例如:76/48 = 1.5(四舍五入到小数点后1位)

Snippet of text file

这是我的代码:

public void studentAverage(double average) throws 
FileNotFoundException 
{
    File infile= new File("qdata.txt");
    Scanner sc = new Scanner(infile);
    double rowNum = 0;
    for (int row = 0; row < arr.length; row++) 
    {
        for (int col = 0; col < arr[row].length; col++) 
        {
            //Im stucked here 
           rowNum += arr[row][col];

        }
        average = rowNum / arr[row].length;
        System.out.println("StudentAverage is: "+average);
        rowNum = 0;
    }
}

5 个答案:

答案 0 :(得分:1)

整个网格中每个值的平均值只是一个数字(我认为)。因此,您所要做的就是取总和,然后除以像元数:

double sum = 0.0d;

for (int row = 0; row < arr.length; row++) {
    for (int col = 0; col < arr[row].length; col++) {
       sum += arr[row][col];
    }
}

int size = arr.length*arr[0].length;
double average = sum / size;
System.out.println("StudentAverage is: " + average);

在计算大小(即学生总数)时,我假设您的2D数组没有锯齿。也就是说,我假设每一行都有相同数量的列数据。

答案 1 :(得分:1)

您可以为此使用流。

获取总体平均值

double average = Arrays.stream(arr)
    .flatMapToInt(Arrays::stream)
    .average();

并获取每行的平均值:

double[] averagePerRow = Ararys.stream(arr)
    .map(Arrays::stream)
    .mapToDouble(IntStream::average)
    .toArray();

这适用于任何2D int数组,无论是否带有锯齿。

答案 2 :(得分:0)

检查此链接-How to find average of elements in 2d array JAVA?

public class AverageElements {
private static double[][] array;

public static void main (String[] args){

    //  Initialize array
    initializeArray();

    //  Calculate average
    System.out.println(getAverage());
}   

private static void initializeArray(){
    array = new double[5][2];
    array[0][0]=1.1;
    array[0][1]=12.3;
    array[1][0]=3.4;
    array[1][1]=5.8;
    array[2][0]=9.8;
    array[2][1]=5.7;
    array[3][0]=4.6;
    array[3][1]=7.45698;
    array[4][0]=1.22;
    array[4][1]=3.1478;
}

private static double getAverage(){
    int counter=0;
    double sum = 0;
    for(int i=0;i<array.length;i++){
        for(int j=0;j<array[i].length;j++){
            sum = sum+array[i][j];
            counter++;
        }
    }

    return sum / counter;
}
}

答案 3 :(得分:0)

double sum=0;
int size=0;
for (int row = 0; row < arr.length; row++) {
    for (int col = 0; col < arr[row].length; col++) {
       sum += arr[row][col];
    }
    size+=arr[row].length;
}

double average = sum/size;
System.out.println("StudentAverage is: "+average);

答案 4 :(得分:0)

我会为此使用列表

public void studentAverage() throws FileNotFoundException {
    File infile= new File("qdata.txt");
    Scanner sc = new Scanner(infile);
    double rowNum = 0;
    List<List<Integer>> grades = new ArrayList<>();
    double totalCount = 0.0;
    while (sc.hasNext()) {
        List<Integer> row = new ArrayList<>();
        Scanner lineScanner= new Scanner(sc.nextLine());
        while (lineScanner.hasNextInt()) {
            row.add(lineScanner.nextInt());
        }
        grades.add(row);
        totalCount += row.size();
    }

    int index = 0;
    for (List<Integer> list : grades) {
        for (Integer grade : list) {
          System.out.println("Student average is " + (double)Math.round(grade.doubleValue() / totalCount * 10) / 10);
        }
    }
}