C# 用名字打高分

C# 用名字打高分,c#,winforms,streamreader,streamwriter,C#,Winforms,Streamreader,Streamwriter,有人会如何在阅读代码时获得高分,包括C#中Windows窗体中的名称? 例如:史蒂夫600 我可以使用StreamReader/Streamwriter获得数字部分,但我无法找到包含名称的方法。有什么建议吗?最简单的方法是将每个值写在自己的行上,因此您可能有: 史蒂夫 600 乔治 500 彼得 二百 然后在循环中,您只需读取一行,即名称,然后读取另一行,并将其解析为int。然后,如果您不在文件末尾,请再次执行此操作。您可以使用特殊的分隔符(如$)将它们分隔开,并且不允许用户在名称中使用分隔符,

有人会如何在阅读代码时获得高分,包括C#中Windows窗体中的名称? 例如:史蒂夫600
我可以使用StreamReader/Streamwriter获得数字部分,但我无法找到包含名称的方法。有什么建议吗?

最简单的方法是将每个值写在自己的行上,因此您可能有:

史蒂夫
600
乔治
500
彼得
二百


然后在循环中,您只需读取一行,即名称,然后读取另一行,并将其解析为
int
。然后,如果您不在文件末尾,请再次执行此操作。

您可以使用特殊的分隔符(如$)将它们分隔开,并且不允许用户在名称中使用分隔符,因此您将有:

史蒂夫600美元


然后,您可以使用方法获取该行字符串,然后使用
string.Split
在分隔符上进行拆分。

您可能希望以固定格式加载其他游戏的分数

如果格式是NAME SCORE,我们将搜索最后一个空格,因为名称可能还包含空格,并在NAME和SCORE部分拆分字符串

    private List<Score> ReadScores(string filename) {
        List<Score> scores = new List<Score>();

        using (var sr = new StreamReader(filename)) {
            string line = "";
            while (!string.IsNullOrEmpty((line = sr.ReadLine()))) {
                int lastspace = line.LastIndexOf(' ');
                string name = line.Substring(0, lastspace);
                string pointstring = line.Substring(lastspace + 1, line.Length - lastspace - 1);

                int points = 0;
                if (!int.TryParse(pointstring, out points))
                    throw new Exception("Wrong format");

                scores.Add(new Score(name, points);

            }
        }

        return scores;
    }

    class Score {
        public string Name { get; set; }
        public int Points { get; set; }

        public Score(string name, int points) {
            this.Name = name;
            this.Points = points;
        }
    }
private List ReadScores(字符串文件名){
列表分数=新列表();
使用(var sr=newstreamreader(文件名)){
字符串行=”;
而(!string.IsNullOrEmpty((line=sr.ReadLine())){
int lastspace=line.LastIndexOf(“”);
string name=line.Substring(0,lastspace);
string pointstring=line.Substring(lastspace+1,line.Length-lastspace-1);
积分=0;
如果(!int.TryParse(pointstring,out points))
抛出新异常(“错误格式”);
分数。添加(新分数(名称、分数);
}
}
返回分数;
}
班级成绩{
公共字符串名称{get;set;}
公共整数点{get;set;}
公共分数(字符串名称,整数分){
this.Name=Name;
这个。点=点;
}
}

如果您不能使用StreamReader或StreamWriter,则可以使用输入框让用户输入自己的姓名。例如:

string sName;


            //asks user to input their name before the game begins
            sName = Microsoft.VisualBasic.Interaction.InputBox("Please enter your name:", "What is Your Name?", "");
            //if no name is entered, they are asked again

                   while (sName == "")
                    {
                    MessageBox.Show("Please enter your name.");
                    sName = Microsoft.VisualBasic.Interaction.InputBox("Please enter your name:", "What is Your Name?", "");
                     }
除了声明变量外,还需要包括

using Microsoft.VisualBasic;
在页面顶部。此外,您还需要添加对页面的引用。在解决方案资源管理器的右侧,如果您右键单击引用,则在.NET中的添加引用将找到“Microsoft.VisualBasic”


您可以将实际的Inpuxbox代码放在代码中的任何位置,并且可以很容易地重复使用和编辑它。

您有什么?winforms中有自己制作的游戏?如果有,请弹出一个窗口,让用户插入他们的名字,并将其放在higscore上方的标签中