游戏高分与名字
人们究竟会如何编写高分阅读代码,其中包括 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法是将每个值写在自己的行上,因此您可能会:
然后在循环中,您只需读取一行(即名称),然后读取另一行,并将其解析为
int
。然后,如果您不在文件末尾,请再次执行相同操作。The simplest way would be to write each value on its own line, so you might have:
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.您可以使用特殊分隔符分隔它们,例如 $ 也不允许用户在名称中使用您的分隔符,因此您将拥有:
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 usestring.Split
to split on delimiter.您可能想以固定格式加载另一个游戏的分数?
如果格式是 NAME SCORE 我们搜索最后一个空格,因为名称可能也可以包含空格并将字符串拆分为名称和分数部分。
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.
如果您无法使用 StreamReader 或 StreamWriter,那么您可以使用输入框让用户输入自己的姓名。例如:
除了声明变量之外,您还需要将其包含
在页面顶部。此外,您还需要添加对该页面的引用。在解决方案资源管理器的右侧,如果右键单击“引用”,即“添加引用”,在 .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:
As well as declaring the variable, you would also need to include
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.