我正在统一地制作一个简单的计分板,可以通过按钮来增加和减少分数,但是无法弄清楚如何将分数排在第一,第二和第三位。
就像在大多数游戏中一样,我只想展示前三项最高得分。即使此示例仅显示了两个分数,此程序仍需要对多达10个分数进行排名。
有什么建议吗?
public class Scores : MonoBehaviour
{
public Text scoreText;
public Text scoreText2;
public Text firstPlace;
public Text secondPlace;
public Text thirdPlace;
public int score;
public int score2;
public void Addition()
{
score++;
scoreText.text = "" + score;
}
public void Subtraction()
{
score--;
scoreText.text = "" + score;
}
private void Update()
{
if (score > score2)
{
firstPlace.text = "Texas A&M";
secondPlace.text = "University of Houston";
thirdPlace.text = "LSU"
}
}
}
实现此目的的更好方法是词典:
// Create a dictionary
public Dictionary <float,string> scores = new Dictionary<float ,string>();
// Add scores you want to add
scores.Add(1,"SomeText");
scores.Add(4,"SomeOtherText");
scores.Add(3,"SomeOtherText");
// convert the keys into a list
float[] order = scores.Keys.ToList();
// sort array in reverse order
order.Sort.Reverse();
/// print the order
Console.Log("First Place"+scores[order[0]]);
Console.Log("Second Place"+scores[order[1]]);
Console.Log("Third Place"+scores[order[2]]);
参考:https://www.dotnetperls.com/sort-dictionaryhttps://www.geeksforgeeks.org/different-ways-to-sort-an-array-in-descending-order-in-c-sharp/
请原谅我的拼写错误。