【问题标题】:Game High Scores with names游戏高分与名字
【发布时间】:2012-02-19 08:57:52
【问题描述】:

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

【问题讨论】:

  • 你有什么?在winforms中自己制作的游戏?如果是这样,为用户创建一个弹出窗口以插入他们的名字并将其放在 higscore 上方的标签中

标签: c# winforms streamreader streamwriter


【解决方案1】:

最简单的方法是将每个值写在自己的行上,所以你可能有:

史蒂夫
600
乔治
500
彼得
200

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

【讨论】:

    【解决方案2】:

    您可以使用特殊的分隔符将它们分开,例如 $ 也不要允许用户在名称中使用您的分隔符,因此您将拥有:

    史蒂夫$600

    然后你可以使用StreamReader.ReadLine方法得到这个行字符串,然后使用string.Split在分隔符上分割。

    【讨论】:

      【解决方案3】:

      您可能想从另一个游戏中以固定格式加载分数?

      如果格式是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;
              }
          }
      

      【讨论】:

        【解决方案4】:

        如果您不能使用 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 代码放在代码中的任何位置,并且可以轻松地重复使用和编辑它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-07
          • 2013-05-19
          • 1970-01-01
          • 1970-01-01
          • 2019-11-25
          • 2011-11-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多