【发布时间】:2016-11-13 15:38:41
【问题描述】:
public class QuoteGenerator
{
public static randomQuote()
{
string t = "Quotes.txt";
List<string> Quotes = new List<string>();
using (StreamReader quoteReader = new StreamReader(t))
{
string line = "";
while ((line = quoteReader.ReadLine()) != null)
{
Quotes.Add(line);
}
}
string[] response = Quotes.ToArray();
string[] shuffle = Classes.RandomStringArrayTool.RandomizeStrings(response);
return (shuffle[0]);
}
}
这是有效的,我认为上面的 StreamReader 代码也可以正常工作:
public string randomQuote()
{
string[] response = new string[] {"The fortune you seek, is in another bot"
, "Someone has Googled you recently"
, "This fortune no good. Try another"
, "404 fortune not found"};
string[] shuffle = Classes.RandomStringArrayTool.RandomizeStrings(response);
return shuffle[0];
}
我需要返回StreamReader方法的第一行引用,为什么我放在一起的代码似乎不起作用?我考虑过对引号进行硬编码,但将它们保存在文本文件中可能是个好主意。我想我不明白使用 StreamReader 是如何工作的。谁能解释一下,我从 7 月才开始编码。谢谢!
【问题讨论】:
-
“为什么我整理的代码似乎不起作用?” -- 不起作用如何? 具体而言是什么您遇到的问题?错误信息?输出不正确?抛出异常?解决您的问题,以便您包含一个好的minimal reproducible example,并清楚、详细地说明问题究竟是什么,以及到目前为止您为解决问题所做的工作。确保包含任何错误或异常消息的准确文本,并完成异常堆栈跟踪。
-
对不起,我的问题缺乏细节,我刚开始编码,事情对我来说有点难以描述。没有错误消息并且代码已编译,只是没有输出。 Ray Wilson 能够指出我需要仔细检查的内容。
标签: c# streamreader