阅读在C#中的两个字符字符、两个

2023-09-06 14:24:14 作者:Ψ×肆战作王

我无法读取Console.Read()方法的第二个字符。我的意思是我没有得到任何提示读取键盘的第二个字符。任何帮助吗?另外,据我所知,字符是默认的int,但我们仍然从输入读取时需要将其转换为char,是不是?下面的code读取的第一个字符,但终止与第二。

 公共静态无效的主要()
    {
        Console.WriteLine(第一个字符?:);
        焦炭firstChar = Convert.ToChar(Console.Read());

        Console.WriteLine(第二个字符?:);
        焦炭secondChar = Convert.ToChar(Console.Read());
    }
 

解决方案

请参阅的 Console.Read 。具体而言,该部分:

  

Read方法阻止它的回报,而你输入的输入字符;它终止,当你preSS Enter键。 pressing进入附加一个依赖于平台的行终止序列,以你的输入(例如,Windows追加一个回车换行符序列)。后续调用Read方法获取一次你输入一个字符。最后一个字符被检索后,再次读取的块的回归和循环重复。

C语言中字符串的两种定义方式

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());
    }

解决方案

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.