游戏高分与名字

发布于 2024-12-29 05:15:40 字数 120 浏览 0 评论 0原文

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

How exactly would someone go about making high score reading code, that includes the name in Windows Form in C#?
For example: Steve 600
I can get the numbers part with StreamReader/Streamwriter, but I cannot figure out a way to include the name. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难理解 2025-01-05 05:15:40

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

史蒂夫
600
乔治
500
彼得
200

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

The simplest way would be to write each value on its own line, so you might have:

Steve
600
George
500
Peter
200

Then in your loop you'd just read one line, which would be the name, then read another line, and parse it into an int. Then if you're not at the end of the file, do the same again.

謌踐踏愛綪 2025-01-05 05:15:40

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

Steve$600

然后您可以使用 StreamReader.ReadLine 方法获取该行字符串并然后使用 string.Split 按分隔符进行拆分。

You can separate them with special delimiter like $ Also don't Allow user to use your delimiter in name, So you will have:

Steve$600

Then you can use StreamReader.ReadLine method to get this line string and then use string.Split to split on delimiter.

昔梦 2025-01-05 05:15:40

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

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

You probably want to load scores from another game with a fixed format?

if the format is NAME SCORE we search for the last space, because the name probably also can contain spaces and split the string in name and score part.

    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;
        }
    }
无所的.畏惧 2025-01-05 05:15:40

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

If you are able to not use StreamReader or StreamWriter then you could use an input box for the user to input their own name. eg:

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?", "");
                     }

As well as declaring the variable, you would also need to include

using Microsoft.VisualBasic;

at the top of your page. And additionally you will need to add a reference to the page. On the right hand side in the Solution Explorer, if you right click on References, the Add Reference, in .NET you will find 'Microsoft.VisualBasic'

You can put the actual Inpuxbox code anywhere in your code and you can re-use and edit it with great ease.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文