问题描述
您好,
我想制作一个测验程序,该程序从包含测验问题的文本文件(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!!
推荐答案
//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());
这篇关于如何从文本文件中选择随机行并将其显示在文本框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!