BigInteger加法始终为0

编程入门 行业动态 更新时间:2024-10-10 15:21:35
本文介绍了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

本文发布于:2023-11-16 03:55:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1600768.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加法   BigInteger

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!