当我收到非数字的输入时,如何发送并出错? [C#]

问题描述 投票:0回答:1

我希望当客户写入符号或字母时出现错误

int n = 0;
string ent;

ent = Console.ReadLine();
n = int.Parse(ent);

if (n.GetType() != typeof(int)) {
    Console.WriteLine("Must be a number");
}

c# visual-studio console syntax-error console-application
1个回答
0
投票

查看官方文档和示例用法:

按照

官方文档
中的建议将int.Parse包装在try-catch块中,或者使用
int.TryParse()
这里

© www.soinside.com 2019 - 2024. All rights reserved.