我有一個遊戲即時通訊問題。我試圖排序高分列表,但它不按預期工作。當我保存比分時,我想進入比賽,我想查看比分高的名單,並檢查它是否高於列表中的分數,如果是的話,將新的高分放入列表中,並將其他分數下移到列表中。 代碼如下所示:如何正確排序我的xml文件?
public void SaveHighScore(int score)
{
// Create the data that needs to be saved
HighScore data = LoadHighScores(HighScoresFilename);
int scoreIndex = 0;
for (int i = 4; i >= 0; i--)
{
if (score >= data.Score[i])
{
scoreIndex = i;
break;
}
}
if (scoreIndex >= 0)
{
int[] tempScore = new int[5];
for (int i = 4; i >= scoreIndex; i--)
{
tempScore[i] = data.Score[i];
}
tempScore[scoreIndex] = score;
for (int i = scoreIndex; i > 0; i--)
{
tempScore[i - 1] = data.Score[i];
}
for(int i = 0; i < data.Score.Length; i++)
data.Score[i] = tempScore[i];
SaveHighScores(data, HighScoresFilename);
}
}
而且我的課是這樣的:https://github.com/PalmerVincent/Game/projects
的問題是,高分列表不能正確,如果一個新的高分被放入數組排序。