如何在 Pascal 中使用字符串集?
我正在编写一个小游戏,其中询问用户他们的种族和类别。 string[5] 有五种可能的竞争,string[9] 有四种可能的类别。
如何对 pascal 进行编程以 1. 将五个种族和四个类定义为常量,2. 检查用户输入以查看输入是否在可能的种族和类内 - 而不使用多个 IF 语句?
任何提示将不胜感激。
I am writing a little game in which the user is asked for their race and class.
There are five possible races of string[5] and four possible classes of string[9].
How do I program pascal to 1. define the five races and four classes as constants, 2. check the user input to see whether the input is within the possible races and classes - without using multiple IF statements?
Any hints will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您的输入是严格定义的,所以我的第一个问题是,您必须使用字符串作为用户输入吗? 不能给用户选择吗? 说下降? 然后您可以将每个选择映射到一个枚举。
上课也一样。
Since your input is strictly defined, my first question is, must you use strings as the user input? Can you not give the user a choice? Say a drop down? Then you can map each choice to an enumeration.
Same for class.
我会推荐 Steves 解决方案作为起点,但进一步使用枚举类型和集合...
您还可以为种族名称和字符类设置常量:
现在,如果您使用组合框进行用户输入和将输入映射到这些枚举类型,检查组合是否有效很简单:
I would recommend Steves solution as the starting point, but go a bit further with the use of enumerated types and sets...
You could also set up constants for the race names and character classes:
Now, if you use comboboxes for user input and map the input to these enumerated types, the check if a combination is valid is simple:
我发现你应该遵循史蒂夫的解决方案。 但如果您喜欢使用字符串,则可以使用 TStringList(在 Delphi/可能是 FreePascal 中)。 您可以用您的比赛填充它,然后使用 TStringList 的 IndexOf 函数评估玩家的答案。 它返回传递的字符串的索引,或者当传递的字符串不在列表中时返回-1。
不管怎样,我强烈推荐史蒂夫的解决方案:)
I find you should follow Steves solution. But if using strings is your way, you could use a TStringList (in Delphi/possibly FreePascal). You could fill it with your races and then evaluate the players answer using the IndexOf function of the TStringList. It returns the index of the passed string or -1 when the passed string isn't in the List.
Anyway i'd strongly recommend Steves solution :)