use*_*206 4 c# command-line input console-application
我试图从控制台读取c#中的一些值,然后处理它们.但是我遇到了错误.
控制台的输入是:
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
Run Code Online (Sandbox Code Playgroud)
等等.我事先不知道控制台输入的数量......我怎样才能检测到用户已经停止输入和存储输入.
我试过用
string line;
while((line=Console.ReadLine())!=null)
{
//but here it seems to be an infinite loop
}
Run Code Online (Sandbox Code Playgroud)
有什么建议
您的代码将查找"控制台输入结束",即"Ctrl + Z",如Console.ReadLine中所述:
如果在方法从控制台读取输入时按下Ctrl + Z字符,则该方法返回null.这使得用户可以在循环中调用ReadLine方法时阻止进一步的键盘输入.以下示例说明了此方案.
如果要查找空字符串,请使用String.IsNullOrWhiteSpace.
string line;
while(!String.IsNullOrWhiteSpace(line=Console.ReadLine()))
{
//but here it seems to be an infinite loop
}
Run Code Online (Sandbox Code Playgroud)