计算最终分数的公式

本文关键字:计算 | 更新日期: 2023-09-27 18:08:12

我被下面最后一个计算难题困住了。我知道如何从正确的可能部分中生成正确部分的百分比分数((correctNumPartsOnBoard/totalpossible ecorrectparts)*100),但我想要最终的百分比分数来考虑板上不正确部分的数量。(即使所有正确的部分都在棋盘上,如果也有错误的部分,你仍然不会得到100%)。现在我现在的公式percentCorrectParts = ((correctNumPartsOnBoard / totalPossibleCorrectParts) / totalNumPartsOnBoard) * 100);是错误的,我很难确定正确的计算。

所以,计算需要工作的方式是:用户需要匹配六种可能的动物之一,每种动物大约有15个正确的部分,但用户也可以将不正确的部分拖到板上(来自其他动物的部分仍然可见,所以他们可以在蜥蜴头上拖动不同的腿或角,他们可以用这种方式制作弗兰肯斯坦式的生物)。所以可用的零件总数是6*15。但鉴于他们并非都是正确的,他们也会通过降低棋盘上棋子的总体平均得分来影响分数。

正确的公式是什么?

// Scoring System
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
public class ScoreManager : MonoBehaviour
{
    public List<string> totalBuildBoardParts;       // Running list of all parts on board (by Tag)
    public int numCorrectPartsOnBoard;
    public int numIncorrectPartsOnBoard;
    public int totalPossibleCorrectParts;
    public float percentCorrectParts;
    void Start()
    {
        GameObject gameController = GameObject.FindGameObjectWithTag("gc");
        GameSetup gameSetup = gameController.GetComponent<GameSetup>();
        totalPossibleCorrectParts = gameSetup.totalPossibleCorrectParts;
        Debug.Log("TOTAL POSSIBLE CORRECT PARTS ARE: " + totalPossibleCorrectParts);
    }
    public void AddAnimalPartByTag(string tag)
    {
        // Add object tag to List
        totalBuildBoardParts.Add(tag);
        Debug.Log ("Added an object tagged as: " + tag);
        GameObject gameController = GameObject.FindGameObjectWithTag("gc");
        GameSetup gameSetup = gameController.GetComponent<GameSetup>();
        if (tag == gameSetup.activeTag)
        {
            numCorrectPartsOnBoard ++;
            Debug.Log ("There are " + numCorrectPartsOnBoard + " correct parts on the board");
        } else {
            numIncorrectPartsOnBoard ++;
        }
        CalculateScore();
    }
    public void RemoveAnimalPartByTag(string tag)
    {
        // Add object tag to List
        totalBuildBoardParts.Remove(tag);
        Debug.Log ("Removed an object tagged as: " + tag);
        GameObject gameController = GameObject.FindGameObjectWithTag("gc");
        GameSetup gameSetup = gameController.GetComponent<GameSetup>();
        if (tag == gameSetup.activeTag)
        {
            numCorrectPartsOnBoard --;
            Debug.Log ("There are " + numCorrectPartsOnBoard + " correct parts on the board");
        } else {
            numIncorrectPartsOnBoard --;
        }
        CalculateScore();
    }
    public void CalculateScore()
    {
        float totalNumPartsOnBoard = totalBuildBoardParts.Count();
        float correctNumPartsOnBoard = numCorrectPartsOnBoard; 
        percentCorrectParts = ((correctNumPartsOnBoard / totalPossibleCorrectParts) / totalNumPartsOnBoard) * 100);
        Debug.Log ("Your current score is: " + percentCorrectParts);
    }
}

计算最终分数的公式

你的公式可能是正确的。但是,您的数据类型不是。

你正在做一个整数除法,结果也是一个int。假设correctNumPartsOnBoard是3,totalPossibleCorrectParts是5,3/5得到0,因为int没有小数。

您需要将除法中的两个操作数之一强制转换为带有小数的数据类型(例如float, doubledecimal):

percentCorrectParts = ((correctNumPartsOnBoard / (float)totalPossibleCorrectParts) / totalNumPartsOnBoard) * 100);

通过将分母totalPossibleCorrectParts设置为浮点数,第一次除法将返回float。然后在第二个分区中使用float,也正确返回float

我认为你的公式应该是这样的:

int correctParts;
int possibleCorrect;
int incorrectParts;
int parts;
float percentFinished =
    Mathf.Max((((float)correctParts/possibleCorrect)   // Correct percent
    - ((float)incorrectParts/parts))                   // Minus incorrect percent
    * 100f,                                            // Normalized to 100
    0f);                                               // Always a minimum of 0

这个公式也不像其他答案,你不需要使用所有的部分来得到100%,只需要得到所有可能正确的部分,而不一定要用尽你所有的部分;)

假设你有100个零件,其中3个正确,3个错误。我们的总目标是20。

int correctParts = 3;
int possibleCorrect = 20;
int incorrectParts = 3;
int parts = 100;
float percentFinished =
    Mathf.Max((((float)correctParts/possibleCorrect)   // Correct percent is 0.15 or 15%
    - ((float)incorrectParts/parts))                   // Minus incorrect percent which is .03 or 3%
    * 100f,                                            // Normalized to 100 which gives us 15% - 3% = 12%
    0f);                                               // Always a minimum of 0

我认为你的最终分数应该是:correctNumPartsOnBoard/totalNumPartsOnBoard * 100

如果你有80个正确的部分和20个不正确的部分,那么总部分是100,你有80个正确的,所以你应该得到80%的分数,像这样:80/(80+20) * 100