我正在为我正在制作的游戏制作排行榜。
我遇到的问题是列表中的所有项目都以相同的值结束。(我的highscores文件最后一行的值)。任何想法如何解决这个问题?
以下是代码:
private void LeaderboardScreen_Load(object sender, EventArgs e)
{
string name = "error";
string age = "error";
string gender = "error";
int score = 0;
List<Player> players = new List<Player>();
using (var fileStream = File.OpenRead(".\\InputInfo.bin"))
using (var streamReader = new StreamReader(fileStream, true))
{
string line;
line = streamReader.ReadLine();
}
foreach (var line in File.ReadLines(".\\InputInfo.bin")) // loop for every line in the inputinfo file
{
string[] words = line.Split(); //Splits the line into seperate words, and puts them into an array called 'words'
name = words[0]; // Makes the first word the name
age = words[1]; //Makes the second word the age
gender = words[2];//Makes the third word the gender
score = Convert.ToInt32(words[3]);//Makes the forth word the score
players.Add(new Player(name,age,gender,score)); **//This is where the problem is I think**
}
players = players.OrderByDescending(i => score).ToList(); //sorts list of players into descending order
}
此外,如果它在这里有帮助,那么该课程:
public class Player
{
public static int score = 0;
public static string name;
public static string age;
public static string gender;
public Player(string aName, string aAge, string aGender, int aScore)
{
name = aName;
age = aAge;
gender = aGender;
score = aScore;
}
}
答案 0 :(得分:3)
删除static
。它表示您的成员由该类的所有实例共享,但您实际上每个实例都需要它们。
答案 1 :(得分:0)
如果您包含文件“InputInfo.bin”会有所帮助。你可以发帖吗?
您还需要更改此行:
players.Add(新玩家(姓名,年龄,性别,得分)); //这就是我认为的问题
对此:
players.Add(新玩家(姓名,年龄,性别,得分)); // **这就是我认为问题的地方**
直到//位于**
之前才会编译