如何从文本文件中选择随机行并将其显示在文本框中? [英] How do I select a random line from a text file and display it in a Text Box?

查看:83
本文介绍了如何从文本文件中选择随机行并将其显示在文本框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我想制作一个测验程序,该程序从包含测验问题的文本文件(QUESTIONS.txt)中随机选择一行。然后,我需要随机选择的行显示在文本文件中。

我单独尝试过这个并且失败了,然后尝试在线搜索但找不到任何有用的东西。



如果有人能给我提供我需要的代码,我会非常感激!

如果您还可以用简单的评论标记代码,只是为了让我知道它是如何工作的我会欣喜若狂!!



提前非常感谢!!

Hello,
I want to make a "quiz" program that randomly selects a line from a text file (QUESTIONS.txt) containing the quiz questions. I then need the randomly selected line to display in a text file.
I have attempted this alone and failed, then tried searching online but couldn't find anything helpful.

If someone could just give me the code that I require, I would be more than grateful!
If you could also label the code with simple comments just to let me know how it works I will be ecstatic!!

Thank you so much in advance!!

推荐答案

让我们思考问题解决过程......

1.将文本文件逐行读取为字符串值< keyvaluepair>< int,>>使用随机整数键

2.按其键排序此列表。

3.将此列表读入数组。

4.循环执行这个数组可以获得随机排序的文本。

接下来,没有汗水没有收获,通过混合和匹配以下链接中的这些示例来解决您的问题:

1. http://www.dotnetperls.com/streamreader [ ^ ]

2. http://www.dotnetperls.com/shuffle [ ^ ]

你可以开始...
Let's ponder over the problem solving process...
1. Read the text file line by line as string values into a List<keyvaluepair><int,>> with randomized integer key
2. Sort this List by its keys.
3. Read this List into an Array.
4. Cycle through this Array to get the randomly sorted texts.
Next, no sweat no gain, get your hands dirty by mixing and matching these examples from the following links to work out your solution:
1. http://www.dotnetperls.com/streamreader[^]
2. http://www.dotnetperls.com/shuffle[^]
You may begin...


我们不是来为你工作。


//Here is something that may help you get started
//You can produce a shuffled List in 3 lines of code
        Random random = new Random();
        var lines=   File.ReadAllLines(@"C:\temp\test.txt");
        var shuffled= lines.OrderBy(line => random.Next());


这篇关于如何从文本文件中选择随机行并将其显示在文本框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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