首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析FormatException时BigInteger

解析FormatException时BigInteger
EN

Stack Overflow用户
提问于 2015-11-15 10:21:56
回答 1查看 1.6K关注 0票数 3

我试图在codewars.com中解决一个问题,在这里我添加了两个BitInteger。这是链接

用我的代码:

代码语言:javascript
复制
using System;
using System.Numerics;
using System.Globalization;

public static class Kata
{
    public static string sumStrings(string a, string b)
    {
      Console.WriteLine(a + " " + b);
      BigInteger numbera = BigInteger.Parse(a);
      BigInteger numberb = BigInteger.Parse(b);
      BigInteger numberc;

      numberc = numbera + numberb;
      Console.WriteLine(numberc);
      return numberc.ToString();
    }
}

我得到一个FormatException-错误:

代码语言:javascript
复制
System.FormatException : The value could not be parsed. 
at System.Numerics.BigNumber.ParseBigInteger (System.String value, NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00000] in <filename unknown>:0 
at System.Numerics.BigInteger.Parse (System.String value) [0x00000] in <filename unknown>:0 
at Kata.sumStrings (System.String a, System.String b) [0x00000] in <filename unknown>:0 
at CodeWarsTest.Test6 () [0x00000] in <filename unknown>:0

但产出似乎是正确的。

代码语言:javascript
复制
input: string 1 --> "712569312664357328695151392" 
input: string 2 --> "8100824045303269669937"
output: sum --> 712577413488402631964821329

为什么我在这里有FormatException??

编辑:代码在MonoDevelopment中工作,但不在Codewars.com网站上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-15 10:50:00

这是他们网站的一个问题。其中一个测试通过第一个参数的无效输入(一个空字符串)。

下面是捕捉到此问题的代码:

代码语言:javascript
复制
BigInteger na, nb;
if (!BigInteger.TryParse(a, out na)) {
    Console.WriteLine("A is invalid: '{0}'",a);
}
if (!BigInteger.TryParse(b, out nb)) {
    Console.WriteLine("B is invalid: '{0}'", b);
}
var nc = na + nb;
return nc.ToString();

印出来

代码语言:javascript
复制
A is invalid: ''

做5号测试。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33718627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档