我正在 HackerRank 中做一些挑战。我通常使用 Visualstudio 中的 windows Form
项目进行调试,但意识到我浪费了很多时间输入测试用例。所以我想要一种可以轻松模拟 console.ReadLine()
通常,挑战的案例描述如下:
5
1 2 1 3 2
3 2
然后读取如下:使用三个 ReadLine
static void Main(String[] args) {
int n = Convert.ToInt32(Console.ReadLine());
string[] squares_temp = Console.ReadLine().Split(' ');
int[] squares = Array.ConvertAll(squares_temp,Int32.Parse);
string[] tokens_d = Console.ReadLine().Split(' ');
int d = Convert.ToInt32(tokens_d[0]);
int m = Convert.ToInt32(tokens_d[1]);
// your code goes here
}
现在我正在考虑创建一个文件 testCase.txt
并使用 StreamReader。
using (StreamReader sr = new StreamReader("testCase.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
这样我就可以用sr.ReadLine()
替换Console.ReadLine()
,但这需要打开一个文本编辑器,删除旧的案例,复制新的案例每次都保存一个文件。
有没有一种方法可以使用文本框,这样只需要在文本框中复制/粘贴并使用streamReader或类似的东西从文本框中读取?
最佳答案
您可以使用StringReader类从字符串
而不是文件中读取。
关于c# - 如何模拟来自控制台的用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42727664/