应用程序因System.FormatException而中断(输入类型不正确?)
本文关键字:类型 输入 不正确 中断 System FormatException 应用程序 | 更新日期: 2023-09-27 17:59:08
我制作了一个树生成器,只是为了训练,我从控制台接受输入,将其拆分为一个数组,但在尝试拆分时,我收到了一个错误,说System.FormatException(不正确的输入类型)
Console.WriteLine("Input Number of branches, treefoot, and branch char");
string[] input = Console.ReadLine().Split(' ');
int nb = int.Parse(input[1]);
char tf = char.Parse(input[2]);
char bc = char.Parse(input[3]);
索引0不是字符串中的int
,而是string
中的文本。
您的索引也有错误,请使用0,1,2
int nb;
bool ok = int.TryParse(input[0], out nb); // ok is true if parsing succeeded
char tf = char.Parse(input[1]);
char bc = char.Parse(input[2]);
您应该使用int.tryParse()
来查看int
解析是否成功。