如何计算平均评分

发布于 2024-11-19 22:55:29 字数 495 浏览 1 评论 0原文

抱歉,如果我不清楚:

我如何根据 MinRate 和 MaxRate 计算平均评分

,例如:minRate = 0 或 1 且 MaxRate = 2 或 3 或 4或 5 或 0

我如何计算评级?

更新结束

我正在实施一个 5 星级评级系统,我的表结构有两列: 1)MinRate 2)MaxRate

所以我的问题是:如何计算平均评分?

以下是我的 5 星级实施情况。

int[] {7, 0, 0, 8, 5}; 

这意味着:

  • 7 位用户对文章评分为 1
  • 0 位用户对文章评分为 2
  • 0 用户对文章评分为 3
  • 8 位用户对文章评分为 4
  • 5 位用户对文章评分为 5

Sorry if I am not clear:

How would I calculate the average rating based on the MinRate and MaxRate

for example: minRate = 0 or 1 and MaxRate = 2 or 3 or 4 or 5 or 0

How would I calculate the rating?

Update End

I am implementing a 5 star rating system and my table structure has two columns:
1) MinRate 2) MaxRate

so my question is: how do calculate the average rating?

here is how I have in my 5 star rating implementation.

int[] {7, 0, 0, 8, 5}; 

This means that:

  • 7 users rated the article with a 1
  • 0 users rated the article with a 2
  • 0 users rated the article with a 3
  • 8 users rated the article with a 4
  • 5 users rated the article with a 5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

青衫负雪 2024-11-26 22:55:29
int[] x =  {7, 0, 0, 8, 5}; 
/* 
    7 x 1 star
    0 x 2 stars
    0 x 3 stars
    8 x 4 stars
    5 x 5 stars
*/

float totalVoters = 0;
float totalPoints = 0;

for(i = 1; i <= x.length; i++)
{
    //add 7, 0, 0, 8, 5
    totalVoters += x[i];

    /* add
        7 x 1 star = 7,
        0 x 2 stars = 0,
        0 x 3 stars = 0,
        8 x 4 stars = 32,
        5 x 5 stars = 25
    */
    totalPoints += x[i] * i;
}

response.write(totalPoints/totalVoters); //get the average
int[] x =  {7, 0, 0, 8, 5}; 
/* 
    7 x 1 star
    0 x 2 stars
    0 x 3 stars
    8 x 4 stars
    5 x 5 stars
*/

float totalVoters = 0;
float totalPoints = 0;

for(i = 1; i <= x.length; i++)
{
    //add 7, 0, 0, 8, 5
    totalVoters += x[i];

    /* add
        7 x 1 star = 7,
        0 x 2 stars = 0,
        0 x 3 stars = 0,
        8 x 4 stars = 32,
        5 x 5 stars = 25
    */
    totalPoints += x[i] * i;
}

response.write(totalPoints/totalVoters); //get the average
野の 2024-11-26 22:55:29

我不太了解 ASP.NET,但平均值只是总数除以项目数:

(int[0] + int[1] * 2 + int[2] * 3 + int[3] * 4 + int[4] * 5) / 5

所以在一个循环中(不起作用,因为 int 被保留,但你得到主意):

float average = 0;

for (int i = minrate; i < maxrate; i++) {
  average += int[i] * i;
}

average /= maxrate;

I don't really know ASP.NET, but the average is just the total divided by the number of items:

(int[0] + int[1] * 2 + int[2] * 3 + int[3] * 4 + int[4] * 5) / 5

So in a loop (won't work, as int is reserved, but you get the idea):

float average = 0;

for (int i = minrate; i < maxrate; i++) {
  average += int[i] * i;
}

average /= maxrate;
浪推晚风 2024-11-26 22:55:29

这是一些伪代码:


double avg = 0;
for (int i = 0; i < arr.Length; i++) // arr.Length should be the same as MaxRate
{
    avg += arr[i] * (i + MinRate);
}
avg /= arr.length;

Here is some pseudo code:


double avg = 0;
for (int i = 0; i < arr.Length; i++) // arr.Length should be the same as MaxRate
{
    avg += arr[i] * (i + MinRate);
}
avg /= arr.length;

寂寞美少年 2024-11-26 22:55:29
((7*1)+(8*4)+(5*5))/(7+8+5) = 3.2
((7*1)+(8*4)+(5*5))/(7+8+5) = 3.2
深者入戏 2024-11-26 22:55:29

这就是我所做的

   private static double GetRating()
        {
            int star5 = 12801;
            int star4 = 4982;
            int star3 = 1251;
            int star2 = 429;
            int star1 = 1265;

            double rating = (double)(5 * star5 + 4 * star4 + 3 * star3 + 2 * star2 + 1 * star1) / (star1 + star2 + star3 + star4 + star5);

            rating = Math.Round(rating, 1);

            return rating;
        }

        static void Main(string[] args)
        {
            double rating = GetRating();
            Console.WriteLine("Your product rating: " + rating);
            Console.ReadKey();
        }

This is what i did

   private static double GetRating()
        {
            int star5 = 12801;
            int star4 = 4982;
            int star3 = 1251;
            int star2 = 429;
            int star1 = 1265;

            double rating = (double)(5 * star5 + 4 * star4 + 3 * star3 + 2 * star2 + 1 * star1) / (star1 + star2 + star3 + star4 + star5);

            rating = Math.Round(rating, 1);

            return rating;
        }

        static void Main(string[] args)
        {
            double rating = GetRating();
            Console.WriteLine("Your product rating: " + rating);
            Console.ReadKey();
        }
风月客 2024-11-26 22:55:29

这是我在 5 票中的 5 票中使用的解决方案:

// 36 = excellent (5), 49 = good (4), 17 = average (3), 5 = bad (2), 9 = terrible (1)
var values = new[] {36, 49, 17, 5, 9};

var result = GetRating(values).ToString(CultureInfo.InvariantCulture);
MessageBox.Show(result); // 3.84

public static double GetRating(int[] ratings)
{
    double ratingsSum = 0;
    double ratingsTotal = 0;
    for (var i = 1; i <= 5; i++)
    {
        ratingsSum += ratings[i - 1];
        ratingsTotal += i * ratings[5 - i];
    }
    var average = ratingsTotal / ratingsSum;
    return Math.Round(average, 2);
}

Here's my solution to use in the 5 of 5 votes:

// 36 = excellent (5), 49 = good (4), 17 = average (3), 5 = bad (2), 9 = terrible (1)
var values = new[] {36, 49, 17, 5, 9};

var result = GetRating(values).ToString(CultureInfo.InvariantCulture);
MessageBox.Show(result); // 3.84

public static double GetRating(int[] ratings)
{
    double ratingsSum = 0;
    double ratingsTotal = 0;
    for (var i = 1; i <= 5; i++)
    {
        ratingsSum += ratings[i - 1];
        ratingsTotal += i * ratings[5 - i];
    }
    var average = ratingsTotal / ratingsSum;
    return Math.Round(average, 2);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文