c#中的char数据类型 [英] char data type in c#
问题描述
在运行以下代码时没有错误,但它不接受第二次输入。
While am running the following code there is no error, but it doesnt accept second input.
Console.Write("Enter first char ");
char a = Convert.ToChar(Console.Read());
Console.Write("Enter second char ");
char b = Convert.ToChar(Console.Read());// this input cannot be accepted
Console.WriteLine("a-" + a.ToString() + ",b-" + b.ToString() + "");
Console.ReadLine();
样本输出:
输入第一个字符c
输入第二个字符,b-
Sample Output:
Enter first char c
Enter second char a-c,b-
推荐答案
如果您只想读取一个字符不要使用Read()
,因为你会遇到更多问题,而不是对你有好处。Console.ReadKey()
是您的理由:
If you ever want to read only a single character don't useRead()
as you'll be getting more problems with it than it's good for you.Console.ReadKey()
is the way to go in your case:
Console.Write("Enter first char: ");
char a = Console.ReadKey().KeyChar;
Console.Write("\nEnter second char: ");
char b = Console.ReadKey().KeyChar;
Console.WriteLine("\na-" + a.ToString() + ",b-" + b.ToString() + "");
Console.ReadLine();
虽然读取将从输入流中获取下一个字符,但它也会阻塞,直到输入新行。这个新行将保留在输入队列中,因此后续的 Read()
将获取该行。请参阅下文,了解MSDN对的说法。读取()
。
MSDN文档有这样的说法:
当您输入输入字符时,Read方法会阻止其返回;当您按下Enter键时,它会终止。按Enter键会将与平台相关的行终止序列添加到您的输入中(例如, Windows追加一个回车换行序列。对Read方法的后续调用一次检索输入一个字符。检索到最后一个字符后,Read再次阻止它返回并重复循环。
请注意,除非执行以下操作之一,否则不会获得属性值-1:同时按下Control修饰键和Z控制台键(Ctrl + Z),这表示文件结束条件;按下等效项表示文件结束条件的键,例如Windows中的F6功能键;或者将输入流重定向到具有实际文件结尾c的源(例如文本文件) haracter。
问候,
While read will fetch the next character from the input stream, it will also block until a new line is entered. This new line will remain in the input queue so your subsequent Read()
will fetch that. See below for what MSDN has to say about Read()
.
The MSDN documentation has this to say:
"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.
Note that you will not get a property value of -1 unless you perform one of the following actions: simultaneously press the Control modifier key and Z console key (Ctrl+Z), which signals the end-of-file condition; press an equivalent key that signals the end-of-file condition, such as the F6 function key in Windows; or redirect the input stream to a source, such as a text file, that has an actual end-of-file character."
Regards,
Console.Write("Enter first char ");
char a = Convert.ToChar(Console.Read());
Console.ReadLine();
Console.Write("Enter second char ");
char b = Convert.ToChar(Console.Read());// this input cannot be accepted
Console.WriteLine("a-" + a.ToString() + ",b-" + b.ToString() + "");
Console.ReadKey(true);
在开始和逐行调试中放置一个断点,你可以看到输出。否则添加Console.Read()以显示预期结果。
Hi,
Place a break point in the start and debug line by line you can see the output. otherwise add the Console.Read() to display the expected result.
Console.Write("Enter first char ");
char a = Convert.ToChar(Console.Read());
Console.Write("Enter second char ");
Console.Read(); // Add this line you can see the expected output
char b = Convert.ToChar(Console.Read());// this input cannot be accepted
Console.WriteLine("a-" + a.ToString() + ",b-" + b.ToString() + "");
Console.ReadLine();
谢谢
Sriram.B
Thanks
Sriram.B
这篇关于c#中的char数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!