等待多行控制台输入完成C#

时间:2020-07-08 01:34:08

标签: c# console console-application

我应该编写一个程序,其中用户输入迷宫,而我的应用程序试图找到一种导航方法。迷宫的输入应该是类似的

#####
#...#
#...#
#####

我应该使用Console.OpenStandardInput(),并且用户副本会将迷宫粘贴到控制台中。但是,当我使用Console.ReadLine()等待用户复制粘贴迷宫时,它仅读取第一行。我知道对于Java,您只需创建一个新的Scanner(System.in),但是如何在c#中执行此操作?

编辑: 我的整个代码是

class Program
{
    static void Main(string[] args)
    {
        StreamReader sr = new StreamReader(Console.OpenStandardInput());
        for (int i = 0; i < 5; i++)
        {
            Console.Write((char)sr.Read());
        }
        Console.WriteLine("done");
        Console.ReadKey();
    }
}

当我复制粘贴时

AB

CD

在记事本中,输出变为

AB

AB

此处是CDcursor

1 个答案:

答案 0 :(得分:0)

Console.ReadLine()在每一行之后返回,因此您需要使用循环从控制台读取所有行,直到得到null

List<string> lines = new List<string>();
string line;
while ((line = Console.ReadLine()) != null)
{
    // Either you do here something with each line separately or
    lines.add(line);
}
// You do something with all of the lines here
相关问题