本文介绍了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 是
这是终端上的内容:
points1 .NumberOfOutcomes(2,3) 前1个 前1个 第二个 第二个 第一个0 第二个 0
points1.NumberOfOutcomes(2, 3) First 1 First 1 Second 1 Second 2 First 0 Second 0 0
推荐答案
这是因为 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
发布评论