简体   繁体   English

我需要关于 JAVA 中二维数组平均评分的代码的帮助

[英]I need help on a code with average rating on 2D array in JAVA

I need help with my code which that my input ratings is我的代码需要帮助,我的输入评级是

-1 2 3 5 -1 5 3 3 1 4 2 2 5 -1 1 3 3 5 4 3
-1 1 1 4 1 3 3 1 2 3 4 -1 4 1 2 4 5 4 2 3
3 -1 2 3 -1 2 5 -1 3 3 5 2 2 1 2 3 5 3 4 2
-1 1 -1 4 1 3 5 2 1 5 3 -1 5 2 1 3 4 5 3 2
-1 -1 3 2 -1 5 5 2 2 4 4 2 3 2 -1 3 4 4 3 1
2 1 1 5 2 2 4 2 3 4 3 -1 5 2 2 5 3 5 2 1
3 -1 3 4 -1 2 5 -1 -1 4 3 -1 3 -1 2 5 5 5 4 2
4 -1 4 2 3 -1 1 3 4 -1 1 4 4 4 -1 2 -1 1 4 4
4 3 3 3 -1 2 2 4 3 -1 2 4 3 4 2 -1 -1 2 2 3
3 -1 3 -1 3 4 -1 5 5 -1 -1 -1 1 -1 -1 1 1 2 -1 5
3 -1 3 4 3 4 -1 5 5 2 3 3 4 1 1 -1 -1 -1 -1 4
4 -1 4 4 1 3 -1 5 4 -1 1 3 4 1 -1 1 -1 1 -1 5
5 -1 3 1 4 3 -1 5 4 1 3 2 1 -1 4 2 1 -1 2 4
3 -1 5 1 4 4 2 5 5 1 2 3 1 1 -1 1 -1 1 -1 5
4 1 5 4 3 -1 1 3 4 -1 -1 3 3 -1 1 1 2 -1 3 5
-1 1 1 3 -1 3 1 3 -1 -1 3 -1 5 2 2 1 4 -1 5 -1
3 -1 2 3 1 5 4 3 3 -1 5 -1 5 2 -1 4 4 3 3 3
1 1 1 3 2 4 1 -1 -1 -1 5 -1 3 -1 -1 1 -1 2 5 2
-1 2 3 5 -1 4 3 1 1 3 3 -1 4 -1 -1 4 3 2 5 1
-1 1 3 3 -1 3 3 1 -1 -1 3 -1 5 -1 -1 3 1 2 4 -1
3 -1 2 4 1 4 3 -1 2 3 4 1 3 -1 2 -1 4 3 5 -1
-1 1 3 5 -1 4 2 1 -1 3 3 2 3 2 -1 3 1 -1 3 -1
3 2 2 3 -1 5 -1 -1 2 3 4 -1 4 1 -1 -1 -1 -1 4 2
-1 3 -1 -1 4 -1 2 -1 2 2 2 5 -1 3 4 -1 -1 2 -1 2
1 4 3 -1 3 2 1 -1 -1 -1 1 3 1 3 3 1 -1 -1 -1 3
4 3 3 -1 4 2 -1 4 -1 -1 2 4 -1 3 4 2 -1 -1 -1 4
-1 5 1 -1 4 1 -1 3 2 2 -1 4 1 3 3 1 -1 -1 -1 3
-1 4 2 1 5 -1 -1 2 1 1 -1 5 -1 5 4 1 2 2 -1 1
2 5 2 -1 3 -1 -1 1 -1 2 -1 4 2 4 3 -1 2 1 -1 -1
2 5 1 1 4 -1 2 1 -1 -1 2 4 -1 3 4 2 -1 -1 -1 4

which I am taking the first role of the rate by vertically which my average[] has 20 values.我在垂直方面扮演了第一个角色,我的平均 [] 有 20 个值。 I am confused about where is wrong with my code such that if number = -1, then it set to 0 and then doesn't count.我对我的代码哪里出了问题感到困惑,如果数字 = -1,那么它设置为 0,然后不算数。 This is a code to count the average rating of a book, my average[] value comes out different then my calculation on a calculator.这是一个计算一本书平均评分的代码,我的平均 [] 值与我在计算器上的计算结果不同。

public static double [] recommandratings(double[][]ratings) {
    double [] average = new double[20];
    int [] count = new int[20];
    double[] sum = new double[30];
    Arrays.fill(count,0);
    Arrays.fill(sum, 0);
    for(int i= 0;i<20;i++) {
        for(int j=0; j<30; j++) {
            if(ratings[j][i] == -1) {
                ratings[j][i] = 0;
                sum[i] += ratings[j][i];
                count[i]--;
            } else {
                sum[i] += ratings[j][i];
                count[i]++;
            }
        }
    }
    for(int a =0; a<20;a++) {
        average[a] = sum[a]/count[a];
    }
    return average;
}

Since you don't want to count number with value -1, there is no use of count[i]-- because you are increasing count only when there is a number not equal to -1.由于您不想计算值为 -1 的数字,因此没有使用 count[i]-- 因为只有当数字不等于 -1 时才会增加计数。

I think the way of iteration of the 2D array is wrong in your case.我认为在您的情况下,二维数组的迭代方式是错误的。 I made the following changes to make it simple and dynamic.我进行了以下更改以使其简单和动态。 I hope the following code will give you desired result.我希望下面的代码会给你想要的结果。

public static double [] recommandratings (double[][]ratings) {
        int rowSize = ratings.length;
        int colSize = ratings[0].length;
        double [] average = new double[colSize];
        int[] count = new int[colSize];
        for (int row= 0;row < rowSize;row++) {
            for (int col=0; col < colSize; col++) {
                if (ratings[row][col] != -1) {
                    average[col] += ratings[row][col];
                    count[col]++;
                }
            }
        }
        for (int a =0; a < colSize;a++) {
            int sum = (int) average[a];
            average[a] = average[a] / count[a];
            System.out.println("count[" + a + "]=" + count[a] + "; sum[" + a + "]=" + sum + "; average[" + a + "]=" + average[a]);
        }
        return average;
}

PS I removed the sum[] and reused the average[] to optimize memory. PS我删除了sum[]并重新使用了average[]来优化 memory。

Update更新

This update is based on the comments where you state that the count[a] is always returning 30, where it should be less than 30. I performed a test with both your exact dataset and a smaller dataset.此更新基于您 state 的评论,即 count[a] 始终返回 30,它应该小于 30。我对您的确切数据集和较小的数据集进行了测试。 I found my solution is returning less than 30 which is expected.我发现我的解决方案返回的结果少于预期的 30。 Therefore, I think you made some mistake in integrating my solution.因此,我认为您在集成我的解决方案时犯了一些错误。 So, I have made a gist with complete source code and input/output as well which will help you integrate it correctly.所以,我用完整的源代码和输入/输出做了一个 要点,这将帮助你正确地集成它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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