是否可以从C#的控制台读取未知数量的行? [英] Is it possible to read unknown number of lines from console in C#?

查看:45
本文介绍了是否可以从C#的控制台读取未知数量的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个函数可以从控制台输入中读取单行( Console.ReadLine()),但是我希望读取或任意数量的行,这在编译时是未知的时间.

There is a function, which can read a single line from the console input (Console.ReadLine()), but I wish to read or some arbitrary number of lines, which is unknown at compile time.

推荐答案

当然可以.只需一次在for循环(如果您知道在开始阅读时需要多少行)或在for循环中一次只读取一行(使用 ReadLine()或您需要的任何其他内容)即可一会儿循环(如果您想在达到 EOF 或某个输入时停止阅读).

Of course it is. Just use just read a single line (using ReadLine() or whatever else you please) at a time within either a for loop (if you know at the beginning of reading how many lines you need) or within a while loop (if you want to stop reading when you reach EOF or a certain input).

确定:

while ((line = Console.ReadLine()) != null) {
    // Do whatever you want here with line
}

这篇关于是否可以从C#的控制台读取未知数量的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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