本文整理汇总了C#中System.Console.Read方法的典型用法代码示例。如果您正苦于以下问题:C# Console.Read方法的具体用法?C# Console.Read怎么用?C# Console.Read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Console
的用法示例。
在下文中一共展示了Console.Read方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
// This example demonstrates the Console.Read() method.
using System;
class Sample
{
public static void Main()
{
string m1 = "\nType a string of text then press Enter. " +
"Type '+' anywhere in the text to quit:\n";
string m2 = "Character '{0}' is hexadecimal 0x{1:x4}.";
string m3 = "Character is hexadecimal 0x{0:x4}.";
char ch;
int x;
//
Console.WriteLine(m1);
do
{
x = Console.Read();
try
{
ch = Convert.ToChar(x);
if (Char.IsWhiteSpace(ch))
{
Console.WriteLine(m3, x);
if (ch == 0x0a)
Console.WriteLine(m1);
}
else
{
Console.WriteLine(m2, ch, x);
}
}
catch (OverflowException e)
{
Console.WriteLine("{0} Value read = {1}.", e.Message, x);
ch = Char.MinValue;
Console.WriteLine(m1);
}
} while (ch != '+');
}
}
输出:
Type a string of text then press Enter. Type '+' anywhere in the text to quit: The quick brown fox. Character 'T' is hexadecimal 0x0054. Character 'h' is hexadecimal 0x0068. Character 'e' is hexadecimal 0x0065. Character is hexadecimal 0x0020. Character 'q' is hexadecimal 0x0071. Character 'u' is hexadecimal 0x0075. Character 'i' is hexadecimal 0x0069. Character 'c' is hexadecimal 0x0063. Character 'k' is hexadecimal 0x006b. Character is hexadecimal 0x0020. Character 'b' is hexadecimal 0x0062. Character 'r' is hexadecimal 0x0072. Character 'o' is hexadecimal 0x006f. Character 'w' is hexadecimal 0x0077. Character 'n' is hexadecimal 0x006e. Character is hexadecimal 0x0020. Character 'f' is hexadecimal 0x0066. Character 'o' is hexadecimal 0x006f. Character 'x' is hexadecimal 0x0078. Character '.' is hexadecimal 0x002e. Character is hexadecimal 0x000d. Character is hexadecimal 0x000a. Type a string of text then press Enter. Type '+' anywhere in the text to quit: ^Z Value was either too large or too small for a character. Value read = -1. Type a string of text then press Enter. Type '+' anywhere in the text to quit: + Character '+' is hexadecimal 0x002b.
示例2: Console.Read()
//引入命名空间
using System;
class MainClass {
public static void Main() {
char ch;
Console.Write("Press a key followed by ENTER: ");
ch = (char) Console.Read(); // get a char
Console.WriteLine("Your key is: " + ch);
}
}