java.math.BigInteger的问题 [英] Problems with java.math.BigInteger
问题描述
我在方法的开头有以下代码:
I have the following code at the head of a method:
BigInteger foo = BigInteger.valueOf(0);
BigInteger triNum = BigInteger.valueOf(0);
//set min value to 1*2*3*4*5*...*199*200.
BigInteger min = BigInteger.ONE;
BigInteger temp = BigInteger.ZERO;
for(int i=1; i<=200; i++)
{
temp = BigInteger.valueOf(i);
min = min.multiply(temp);
}
System.out.println(min);
while(triNum.compareTo(min) <= 0)
{
foo.add(BigInteger.ONE);
triNum = triNum.add(foo);
System.out.println("triNum: "+triNum);
}
这应该是将最小值加载到一个值(1 * 2 * 3 * ... * 199 * 200),然后将triNum设置为第一个*三角形数字**,且其值大于min.
This is supposed to load a min to a value (1 * 2 * 3 * ... * 199 * 200), and then set triNum to the first *triangle number** with a value greater than min.
问题是,当我运行该方法时,我得到的只是一个终端窗口,该窗口带有"triNum:0"列表,一直在屏幕上向下滚动...我的代码中什么也看不到(尽管它完全是可能是我犯了一些错误,并且我对math.BigInteger有点不熟悉),这似乎可以追溯到BigInteger类.有人在我的代码中看到错误了吗?
Problem is, when I run the method, all I get is a terminal window with a list of "triNum: 0" ever scrolling down the screen... I don't see anything in my code (although it is completely possible I made some mistake, and I am somewhat unfamiliar with math.BigInteger), and this seems to point back to the BigInteger class. Anyone see a bug in my code?
............................................... ................................................... .........................
..........................................................................................................................
*三角形数字是可以通过以下方式获得的数字:1 + 2 + 3 + 4 + 5 + 6 + 7 + ...
*A triangle number is a number that can be reached by: 1+2+3+4+5+6+7+...
推荐答案
查看
foo.add(BigInteger.ONE);
此更新foo
吗?还是创建一个等于foo+ BigInteger.ONE
且不再使用的对象?
Does this update foo
? Or does it create an object that's equal to foo+ BigInteger.ONE
which is not used again?
这篇关于java.math.BigInteger的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!