BigInteger加法始终为0

BigInteger加法始终为0

本文介绍了BigInteger加法始终为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:当尝试将BigIntegers加起来时,结果仍然为0。

I have the following issue: when trying to add to a sum of BigIntegers the outcome remains 0.

这里是代码:

 public void NumberOfOutcomes(int x, int y){
   BigInteger first = BigInteger.valueOf(0);
   BigInteger second = BigInteger.valueOf(0);
   for(int i = 0; i <= (x / 2); i++){
       first.add( fac(x - i).divide((fac(x - 2*i).multiply(fac(i)))) );
       System.out.println("First " + first.add( fac(x - i).divide((fac(x - 2*i).multiply(fac(i)))) ));
    }

   for(int i = 0; i <= (y / 2); i++){
       second.add( fac(y - i).divide((fac(y - 2*i).multiply(fac(i)))) );
       System.out.println("Second " + second.add( fac(y - i).divide((fac(y - 2*i).multiply(fac(i)))) ));
    }
   System.out.println("First " + first);
   System.out.println("Second " + second);
   System.out.println(first.multiply(second));
   }

此处 fac

这是终端上的内容:


推荐答案

这是因为 BigInteger 不可变的,这意味着其值不会更改。因此 first.add(x)将创建新的 BigInteger ,其中包含计算结果,也就是说,只需将结果重新分配为first,例如 first = first.add(...)

This is because BigInteger is immutable which means that its value does not change. So first.add(x) will create a new BigInteger containing the computations result, i.e. just reassign the result to first, like first = first.add(...).

这篇关于BigInteger加法始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 17:08