Console.Read()方法用于从标准输入流中读取下一个字符。当用户键入一些输入字符时,此方法基本上会阻止其返回。用户一旦按ENTER键,它将终止。
Syntax: public static int Read ();
Return Value: It returns the next character from the input stream, or a negative one (-1) if there are currently no more characters to be read.
Exception: This method will give IOException if an I/O error occurred.
下面的程序说明了上面讨论的方法的使用:
范例1:
// C# program to illustrate the use
// of Console.Read Method
using System;
namespace GFG {
class Program {
static void Main(string[] args)
{
int x;
Console.WriteLine("Enter your Character to get Decimal number");
// using the method
x = Console.Read();
Console.WriteLine(x);
}
}
}
输出:
范例2:
// C# program to illustrate the use
// of Console.Read Method
using System;
namespace GFG {
class Program {
static void Main(string[] args)
{
// Write to console window.
int x;
Console.WriteLine("Enter your Character to get Decimal number");
x = Console.Read();
Console.WriteLine(x);
// Converting the decimal into character.
Console.WriteLine(Convert.ToChar(x));
}
}
}
输出:
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.console.read?view=netframework-4.7.2