📜  Java 8 | BigInteger divideAndRemainder() 方法与示例

📅  最后修改于: 2022-05-13 01:55:45.578000             🧑  作者: Mango

Java 8 | BigInteger divideAndRemainder() 方法与示例

Java.math.BigInteger.divideAndRemainder(BigInteger val)是在Java 8 中引入的。在调用此方法的 BigInteger 和作为参数传递给另一个方法的 BigInteger 之间应用除法运算后,此方法返回一个包含两个 BigInteger 的数组。这里,数组的第一个 BigInteger 表示除法的结果(this / val),第二个 BigInteger 表示该除法运算的余数(this % val)。

句法:

public BigInteger[] divideAndRemainder(BigInteger val)

参数:该方法需要一个 BigInteger 类型的强制参数val ,它作为除数来应用除法运算。

返回值:此方法返回一个由两个 BigInteger 组成的数组,其中商 (this / val) 作为第一个元素,余数 (this % val) 作为第二个元素。

异常:如果传入参数为 null 或 0,该方法将抛出ArithmeticException

例子:

Input: 42245, 23
Output: [1836, 17]

Input: 25556, 444
Output: [57, 248]

Explanation: In input two BigInteger are given.
The first one is the dividend, which calls the method,
and the second is the divisor, which is passed as parameter.
When applying division operation, as a result, 
an array of quotient and remainder is returned as output.
These are also of BigInteger type

下面的程序说明了 BigInteger 类的 divideAndRemainder() 方法:

方案一:

// Java program to demonstrate divideAndRemainder() method
  
import java.math.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Creating a BigInteger object
        BigInteger dividend = new BigInteger("25556");
        BigInteger divisor = new BigInteger("444");
  
        // applying divideAndRemainder() method
        BigInteger[] answers = dividend.divideAndRemainder(divisor);
  
        // print results
        System.out.println("Dividend : " + dividend);
        System.out.println("Divisor : " + divisor);
        System.out.println("Quotient  : " + answers[0]);
        System.out.println("Remainder : " + answers[1]);
    }
}
输出:
Dividend : 25556
Divisor : 444
Quotient  : 57
Remainder : 248

方案二:

// Java program to demonstrate divideAndRemainder() method
  
import java.math.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Creating a BigInteger object
        BigInteger dividend = new BigInteger("32345678987");
        BigInteger divisor = new BigInteger("1537862842");
  
        // applying divideAndRemainder() method
        BigInteger[] answers = dividend.divideAndRemainder(divisor);
  
        // print results
        System.out.println("Dividend : " + dividend);
        System.out.println("Divisor : " + divisor);
        System.out.println("Quotient  : " + answers[0]);
        System.out.println("Remainder : " + answers[1]);
    }
}
输出:
Dividend : 32345678987
Divisor : 1537862842
Quotient  : 21
Remainder : 50559305

程序 3:演示 ArithmeticException

// Java program to demonstrate divideAndRemainder() method
  
import java.math.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Creating a BigInteger object
        BigInteger dividend = new BigInteger("32345678987");
  
        // Creating 0 as divisor
        BigInteger divisor = new BigInteger("0");
  
        // print both numbers
        System.out.println("Dividend : " + dividend);
        System.out.println("Divisor : " + divisor);
  
        try {
            // applying divideAndRemainder() method
            BigInteger[] answers = dividend.divideAndRemainder(divisor);
  
            // print results
            System.out.println("Quotient  : " + answers[0]);
            System.out.println("Remainder : " + answers[1]);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
输出:
Dividend : 32345678987
Divisor : 0
Exception: java.lang.ArithmeticException: BigInteger divide by zero

参考: https: Java/math/BigInteger.html#divideAndRemainder-java.math.BigInteger-