gpt4 book ai didi

c# - 在c#中从控制台输入多行

转载 作者:太空狗 更新时间:2023-10-30 00:31:37 24 4
gpt4 key购买 nike

我正在尝试从控制台读取 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

等等。我事先不知道控制台输入的数量...我如何检测到用户已停止输入并存储输入。

我试过用

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

任何建议

最佳答案

您的代码查找“控制台输入结束”,即 Console.ReadLine 中所述的“Ctrl+Z” :

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 .

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

关于c# - 在c#中从控制台输入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24338657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com