当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java 8 BigInteger divideAndRemainder()用法及代码示例


Java 8中引入了java.math.BigInteger.divideAndRemainder(BigInteger val)。此方法在调用此方法的BigInteger与作为参数传递给另一个方法的BigInteger之间进行除法运算后,返回两个BigInteger的数组。在此,数组的第一个BigInteger表示除法的结果(this /val),第二个BigInteger表示该除法运算的其余部分(此%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()方法:

程序1:

// 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

程序2:

// 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://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#divideAndRemainder-java.math.BigInteger-



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Java 8 | BigInteger divideAndRemainder() method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。