我有以下代码:
List<int> moneys = new List<int>();
Console.WriteLine("Please enter the cost of your choice");
int money = int.Parse(Console.ReadLine());
moneys.Add(money);
如果您从这里输入文本,程序将停止工作并出现未处理的异常消息。我想知道您将如何处理异常,如果有可能使程序不停止工作?
最佳答案
你应该使用 TryParse方法。如果输入无效,它不会抛出异常。这样做
int money;
if(int.TryParse(Console.ReadLine(), out money))
moneys.Add(money);
关于c# - List<int> 中用户输入的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18105399/