为什么c#控制台不能输入数字

按键盘上的数字不能读入...
2026年09月22日 21:36
有2个网友回答
网友(1):

用正则表达式

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string a = "";
a = Console.ReadLine();
while (!IsNum(a))
{
Console.WriteLine("非数字,请重新输入。");
a = Console.ReadLine();
Console.WriteLine("刚才输入的内容为:" + a);
Console.ReadLine();
}
}
private static bool IsNum(string s)
{
return Regex.IsMatch(s, @"^\d+");
}

}
}

即可

网友(2):

如果是小键盘的话 看一下NumLock有没有开。描述太模糊,请附上代码