[英]Java nested biginteger loop is not working
我想用java的biginteger創建一個嵌套循環,但是我遇到了第一個循環的問題。它仍然停滯不前,我試圖打印它,它總是2
而不加1
Scanner in=new Scanner(System.in);
BigInteger y=in.nextBigInteger();
BigInteger h=BigInteger.valueOf(1);
for(BigInteger i=BigInteger.valueOf(2);i.compareTo(y)<=0;i.add(BigInteger.ONE)){
h=i;
System.out.println(i);
for(BigInteger j=i;j.compareTo(y)<=0;j.add(BigInteger.ONE)){
h=h.multiply(j);
if(h.compareTo(y)==0){
System.out.println(j+" "+i);
}else if(h.compareTo(y)>0){
break;
}
}
}
}
}
從文檔中:
加(BigInteger val)
返回其值為(this + val)的BigInteger。
因此, i.add(1)
行為與i++
行為不同, BigInteger
是不可變的。 您需要改用i = i.add(BigInteger.ONE)
,它應該可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.