BigInteger可表示任意大小的整数,使用new BigInteger(String)来创建。
BigInteger bi = new BigInteger("12321321312312");//创建一个类
可用add()、subtract()、multiply()、divide()、remainder()方法对定义的大整数进行计算。
上面的方法依此表示加、减、乘、除、比较两数的大小并输出较小的数。
请看下方示例:
import java.math.BigInteger;
import java.util.Scanner;
public class Biginteger {
public static void main(String[] args) {
Scanner Sc = new Scanner(System.in);
BigInteger a = new BigInteger("9");
BigInteger b = new BigInteger(Sc.next());
BigInteger c = a.add(b);//计算a + b
BigInteger d = a.subtract(b);//计算a - b
BigInteger e = a.multiply(b);//计算a * b
BigInteger f = a.divide(b);//计算a / b
BigInteger g = a.remainder(b);//比较并输出较小的数字
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
}
}
输入输出: