C# 读取两个字符
我无法使用 Console.Read() 方法读取第二个字符。我的意思是我没有收到任何从键盘读取第二个字符的提示。有什么帮助吗?另外,我知道字符默认是 int 但我们在从输入读取时仍然需要将其转换为 char,对吗?下面的代码读取第一个字符,但以第二个字符终止。
public static void Main()
{
Console.WriteLine("The First Character?:");
char firstChar=Convert.ToChar(Console.Read());
Console.WriteLine("The Second Character?:");
char secondChar=Convert.ToChar(Console.Read());
}
I cannot read a second character with Console.Read() method. I mean I don't get any Prompt to read the second character from Keyboard. Any help please? Also, I understand that character is by default an int but we still need to convert it to char when reading from input, is it right? The code below reads the first char but terminates with the second.
public static void Main()
{
Console.WriteLine("The First Character?:");
char firstChar=Convert.ToChar(Console.Read());
Console.WriteLine("The Second Character?:");
char secondChar=Convert.ToChar(Console.Read());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来
Console.ReadKey()
是您真正想要的。Looks like
Console.ReadKey()
is what you actually want.请参阅Console.Read。具体来说,这部分:
Please see Console.Read. Specifically, this part:
也许这段代码更接近您正在寻找的内容......
Perhaps this code is closer to what you're looking for...
您的第二个
Console.Read()
正在消耗回车符来终止第一次读取。Console.ReadKey
使用起来更友好一些,因为它不需要终止回车符。如果您想继续使用Console.Read,您可以尝试在第二个提示之前使用并丢弃回车符,例如:Your second
Console.Read()
is consuming the carriage return terminating the first read.Console.ReadKey
is a bit friendlier to use, since it doesn't require a terminating carriage return. If you want to continue usingConsole.Read
, you might try consuming and discarding the carriage return before the second prompt, like: