C# 读取两个字符

发布于 2024-10-20 00:07:49 字数 419 浏览 6 评论 0原文

我无法使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

汐鸠 2024-10-27 00:07:49

看起来 Console.ReadKey() 是您真正想要的。

Looks like Console.ReadKey() is what you actually want.

金橙橙 2024-10-27 00:07:49

请参阅Console.Read。具体来说,这部分:

当您键入输入字符时,Read 方法会阻止其返回;当您按 Enter 键时,它会终止。按 Enter 键会将与平台相关的行终止序列附加到您的输入中(例如,Windows 会附加回车换行序列)。对 Read 方法的后续调用一次检索一个字符。检索到最后一个字符后,Read 再次阻止其返回并重复循环。

Please see Console.Read. Specifically, this part:

The Read method blocks its return while you type input characters; it terminates when you press the Enter key. Pressing Enter appends a platform-dependent line termination sequence to your input (for example, Windows appends a carriage return-linefeed sequence). Subsequent calls to the Read method retrieve your input one character at a time. After the final character is retrieved, Read blocks its return again and the cycle repeats.

紫﹏色ふ单纯 2024-10-27 00:07:49

也许这段代码更接近您正在寻找的内容......

public static void Main()
{
        Console.WriteLine("The First Character?:");
        char firstChar = Convert.ToChar(Console.ReadKey().KeyChar);
        Console.WriteLine();
        Console.WriteLine("The Second Character?:");
        char secondChar = Convert.ToChar(Console.ReadKey().KeyChar);
}

Perhaps this code is closer to what you're looking for...

public static void Main()
{
        Console.WriteLine("The First Character?:");
        char firstChar = Convert.ToChar(Console.ReadKey().KeyChar);
        Console.WriteLine();
        Console.WriteLine("The Second Character?:");
        char secondChar = Convert.ToChar(Console.ReadKey().KeyChar);
}
栀子花开つ 2024-10-27 00:07:49

您的第二个 Console.Read() 正在消耗回车符来终止第一次读取。

Console.ReadKey 使用起来更友好一些,因为它不需要终止回车符。如果您想继续使用Console.Read,您可以尝试在第二个提示之前使用并丢弃回车符,例如:

public static void Main()
{
    Console.WriteLine("The First Character?:");
    char firstChar = Convert.ToChar(Console.Read());

    Console.Read(); // consume carriage return

    Console.WriteLine("The Second Character?:");
    char secondChar = Convert.ToChar(Console.Read());

    Console.WriteLine(secondChar);
}

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 using Console.Read, you might try consuming and discarding the carriage return before the second prompt, like:

public static void Main()
{
    Console.WriteLine("The First Character?:");
    char firstChar = Convert.ToChar(Console.Read());

    Console.Read(); // consume carriage return

    Console.WriteLine("The Second Character?:");
    char secondChar = Convert.ToChar(Console.Read());

    Console.WriteLine(secondChar);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文