阅读在C#中的两个字符 [英] Reading two Characters in C#

查看:120
本文介绍了阅读在C#中的两个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法读取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方法获取一次你输入一个字符。最后一个字符被检索后,再次读取的块的回归和循环重复。

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.

这篇关于阅读在C#中的两个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆