用户实现自由输入数字 最大25位数 检验用户是否正常输入
时间: 2024-02-27 17:56:32 浏览: 23
你可以使用 C# 中的 `BigInteger` 类来表示大整数,并使用 `Regex` 类来验证用户输入是否为数字。以下是一个示例代码:
```
using System;
using System.Numerics;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个数字:");
string input = Console.ReadLine();
// 验证用户输入是否为数字
Regex regex = new Regex(@"^\d{1,25}$");
if (!regex.IsMatch(input))
{
Console.WriteLine("输入的不是有效的数字!");
return;
}
BigInteger number = BigInteger.Parse(input);
Console.WriteLine("输入的数字为:" + number);
}
}
```
在这个例子中,我们首先使用 `Console.ReadLine()` 函数获取用户输入。然后,我们使用 `Regex` 类中的正则表达式验证用户输入是否为 1 到 25 位数字。如果输入不是数字,则输出错误消息并退出程序。否则,我们将字符串转换为 `BigInteger` 类型表示的大整数,并在控制台上输出它。