如何使用名称保存数组或列表,将其加载并选择随机值? [英] How do I save an array or list with a name, load it back and pick a random value?

查看:56
本文介绍了如何使用名称保存数组或列表,将其加载并选择随机值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!这个你能帮我吗。我正在尝试创建一个简单的应用程序,它将从预定义的组列表中生成一个随机名称。必须首先在应用程序中输入列表,因此最好将列表保存在单个文件中,并且可以将项目添加到列表中或在某些时候删除;虽然在这一点上我只需要保存一个列表然后重新加载一个特定的组。



所以说例如我的组合水果的内容为苹果,葡萄,甜瓜,西瓜,菠萝;

然后另一个动物列表:猫,狗,老鼠,牛等等。



然后在应用程序中用户可以选择将使用一组列表并使用按钮单击该组中的随机项目。



提前感谢您。



我尝试了什么:



Hello! Please help me out with this. I am trying to create a simple application that will generate a random name from a pre-defined group lists. The lists will have to be entered first in the application so preferably the lists to be saved in a single file and that items can be added to the list or deleted at some point; though at this point I just needed to save a list and reload it a specific group.

So say for example I have the group fruits with contents as apple, grapes, melon, watermelon, pineapple;
then another list of animals: cat, dog, rat, cow and so on.

Then in the application the user can choose which group of list to use and with a button click a random item from that group will be chosen.

Thank you in advance.

What I have tried:

I already have ideas about how to generate the random item from the list. I tried tinkering with  INI file  but it seems not to work with me since usually INI files contains key-pair and section value whereas my list will just be a plain list of name with a group name for that list.

推荐答案

不要使用INI文件(INI代表初始化)来存储数据。使用XML文件。



0)创建一个派生自 List< string> 的模型,该模型还包含一个字符串团体名称的属性。



1)创建一个表格,允许你添加/编辑/删除团体物品



2)向使用内置.Net XML序列化程序的模型添加代码以保存和检索组。



3)添加代码以供使用/显示检索到的项目。



作为程序员的一部分是能够将需求分解为逻辑部分。如果你不能做到这一点,你将成为一名程序员而失败的话。
Don't use the INI file ("INI" stands for "initialization") to store data. Use an XML file.

0) Create a model derived from List<string> that also contains a string property for the group name.

1) Create a form that allows you to add/edit/delete group items

2) Add code to the model that uses the built-in .Net XML serializer to save and retrieve the groups.

3) Add code to use/display the retrieved items.

Part of being a programmer is being able to break a requirement down into its logical parts. If you can't do this you're going to fail rather spectacularly at being a programmer.


第一步是定义一种文件格式,用于存储数据,同时已经记住如何数据可以简单地通过代码读取和写入。这可能是自定义的或现有格式(例如,提到的INI文件或XML)。



现有格式的优点是通常已有读写实现。



但是这样的实现可能对你的情况来说太复杂了(例如XML解析器)。



逐行读写文本文件非常简单。所以这可能是你的最佳选择,也是一个很好的练习。



一个示例文件格式可能是:



  • 空行被忽略
  • 可选:以(在此处选择非字母字符)开头的行被视为注释并被忽略
  • 一个组由 GROUP:语句启动,后跟一行中的名称:GROUP:groupname
  • 以下所有行(如果不为空或注释)被视为组成员,直到发生另一个GROUP语句或文件结束为止
The first step is defining a file format for storing the data while already having in mind how the data can be simply read and written by code. This might be self-defined or an existing format (e.g. the mentioned INI files or XML for example).

The advantage of existing formats is that there is usually already an implementation for reading and writing.

But such implementations might be too complex for your case (e.g. an XML parser).

Reading and writing text files line-by-line is quite simple. So this might be the best choice in your case and is also a good exercise.

An example file format might be:

  • Empty lines are ignored
  • Optional: Lines beginning with (choose non-alpha character(s) here) are treated as comments and are ignored
  • A group is started by a GROUP: statement followed by the name in a single line: "GROUP:groupname"
  • All following lines (if not empty or a comment) are treated as group members until another GROUP statement occurs or the end of the file is reached


这篇关于如何使用名称保存数组或列表,将其加载并选择随机值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆