BigInteger.intValue();返回一个int值。Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types). BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.
package csdn;import java.math.BigInteger;public class IntegerTest { public static void main(String[] args) { int a = 10; BigInteger b = BigInteger.valueOf(a); // int -> BigInteger System.out.println(b);
BigInteger c = BigInteger.valueOf(100);
// 1 - 100 中的质数 有的麻烦 ... 仅供参考 BigInteger是可以用for循环的 for (BigInteger i = BigInteger.valueOf(2); i.compareTo(c) < 0; i = i.add(BigInteger.valueOf(1))) { boolean flag = true; for (BigInteger j = BigInteger.valueOf(2); j.compareTo(BigInteger.valueOf(Long.parseLong((int)Math.sqrt(Double.parseDouble(i+""))+""))) <= 0; j = j.add(BigInteger.valueOf(1))) { if (i.mod(j) == BigInteger.ZERO) { flag = false; } } if (flag) System.out.print(i + " "); } } }
谢了
在对象里使用for循环?没理解
BigInteger他是一个类,不是基本数据类型。
package csdn;import java.math.BigInteger;public class IntegerTest {
public static void main(String[] args) {
int a = 10;
BigInteger b = BigInteger.valueOf(a); // int -> BigInteger
System.out.println(b);
BigInteger c = BigInteger.valueOf(100);
// 1 - 100 中的质数 有的麻烦 ... 仅供参考 BigInteger是可以用for循环的
for (BigInteger i = BigInteger.valueOf(2); i.compareTo(c) < 0; i = i.add(BigInteger.valueOf(1))) {
boolean flag = true;
for (BigInteger j = BigInteger.valueOf(2); j.compareTo(BigInteger.valueOf(Long.parseLong((int)Math.sqrt(Double.parseDouble(i+""))+""))) <= 0; j = j.add(BigInteger.valueOf(1))) {
if (i.mod(j) == BigInteger.ZERO) {
flag = false;
}
}
if (flag)
System.out.print(i + " ");
}
}
}