繁体   English   中英

解析负数时出现FormatException

[英]FormatException when parsing a negative number

所以我有这段代码:

String inputValues = Console.ReadLine();
string[] values = inputValues.Split(" ");
int firstValue = Int32.Parse(values[0]);
int secondValue = Int32.Parse(values[1]);
int thirdValue = Int32.Parse(values[2]);

不知何故,我在firstvalue-value上抛出了formatexception,说当我在其中放置负数(如-2)时,字符串格式不正确。 我在控制台中也放了一些奇怪的括号,将值放在其中。您可以在图片中看到它们。 这些是什么,它们是否在做我的值,因为可以肯定您可以解析负数?

在此处输入图片说明

尝试这个。

        String inputValues = Console.ReadLine();

        var formatSign = new NumberFormatInfo();
        formatSign.NegativeSign = "−";

        string[] values = inputValues.Split(" ");
        int firstValue = Int32.Parse(values[0],formatSign);
        int secondValue = Int32.Parse(values[1],formatSign);
        int thirdValue = Int32.Parse(values[2],formatSign);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM