示例代码:
class Program
{
static void Main(string[] args)
{
while (true)
{
Test();
}
Console.ReadKey(true);
}
static void Test()
{
ConsoleKeyInfo keyinfo = Console.ReadKey(true);
int n;
if (int.TryParse(keyinfo.KeyChar.ToString(), out n))
{
Console.WriteLine("你选择了:"+n);
}
else
{
Console.WriteLine("请按数字键选择");
}
}
}
bool bIsNumber = false;
int d;
char ch;
int i = Console.Read();//读取一个文字
ch = Convert.ToChar(i);
bIsNumber= Int32.TryParse(ch.ToString(), d);
//判断是否为数字
class Program
{
static void Main(string[] args)
{
while (true)
{
Test();
}
Console.ReadKey(true);
}
static void Test()
{
ConsoleKeyInfo keyinfo = Console.ReadKey(true);
int n;
if (int.TryParse(keyinfo.KeyChar.ToString(), out n))
{
Console.WriteLine("你选择了:"+n);
}
else
{
Console.WriteLine("请按数字键选择");
}
}
}
try
{
for(int IntNumber=0;IntNumber<=9;IntNumber++)
{
IntNumber = Concole.ReadLine();
Console.WriteLine(IntNumber);
}
}
catch(Expetion ex)
{Console.WriteLine(ex);}
while (true)
{
ConsoleKeyInfo i = Console.ReadKey(true);
char c = i.KeyChar;
if (Char.IsNumber(c))
{
Console.WriteLine(c.ToString());
}
}