在C#中从控制台输入多行 [英] Multiple lines input from console in c#

查看:115
本文介绍了在C#中从控制台输入多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从控制台读取c#中的某些值,然后对其进行处理.但是我陷入了错误.

I am trying to read some values in c# from console and then processing them. However i am stuck at a error.

控制台输入为:

Name:ABCD
School:Xyz
Marks:80
 //here the user enters a new line before entering new data again
Name:AB
School:Xyz
Marks:90
//new line again 
Name:AB
School:Xyz
Marks:90

,依此类推.我事先不知道控制台输入的数量...我该如何检测到该用户已停止输入&存储输入.

and so on. I do not know before hand the number of console inputs...How can i detect that user has stopped entering & store the input.

我尝试使用

string line;
while((line=Console.ReadLine())!=null)
{
  //but here it seems to be an infinite loop 
}

任何建议

推荐答案

您的代码将查找控制台输入结束",即

Your code looks for "end of console input" which is "Ctrl+Z" as covered in Console.ReadLine:

如果在从控制台读取输入时按Ctrl + Z字符,则该方法返回null.当循环调用ReadLine方法时,这使用户可以防止进一步的键盘输入.以下示例说明了这种情况.

If the Ctrl+Z character is pressed when the method is reading input from the console, the method returns null. This enables the user to prevent further keyboard input when the ReadLine method is called in a loop. The following example illustrates this scenario.

如果您要查找空字符串作为完成内容,请使用 String.IsNullOrWhiteSpace .

If you are looking for empty string as completion use String.IsNullOrWhiteSpace.

string line;
while(!String.IsNullOrWhiteSpace(line=Console.ReadLine()))
{
  //but here it seems to be an infinite loop 
}

这篇关于在C#中从控制台输入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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