當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。