BigInteger类divideAndRemainder()方法 (BigInteger Class divideAndRemainder() method)
divideAndRemainder() method is available in java.math package.
splitAndRemainder()方法在java.math包中可用。
divideAndRemainder() method returns BigInteger array of 2 elements that contain quotient that is calculated by using (this BigInteger)/(BigInteger val) followed by the remainder that is calculated by using (this BigInteger) % (BigInteger val).
splitAndRemainder()方法将返回由2个元素组成的BigInteger数组,其中包含使用(this BigInteger)/(BigInteger val)计算得出的商数,然后返回使用(this BigInteger)%(BigInteger val)计算得出的余数。
divideAndRemainder() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
splitAndRemainder()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
divideAndRemainder() method may throw an exception at the time of calculating the remainder.
在计算余数时, divideAndRemainder()方法可能会引发异常。
ArithmeticException: This exception may throw when the given parameter holds value 0.
ArithmeticException:当给定参数的值为0时,可能引发此异常。
Syntax:
句法:
public BigInteger[] divideAndRemainder(BigInteger val);
Parameter(s):
参数:
BigInteger val – represents the value by which this BigInteger is to be divided and generate remainder.
BigInteger val –表示该BigInteger被除以并生成余数的值。
Return value:
返回值:
The return type of this method is BigInteger[], it returns an array of BigInteger of two element of "BigInteger" type and the quotient is calculated by using (this BigInteger)/ (BigInteger val) and the remainder is calculated by using (this BigInteger) % (BigInteger val).
此方法的返回类型为BigInteger [] ,它返回两个由BigInteger类型组成的元素的BigInteger数组,商是使用(this BigInteger)/(BigInteger val)计算的,余数是使用(this BigInteger)%(BigInteger val)。
Example:
例:
// Java program to demonstrate the example
// of divideAndRemainder(BigInteger val) method of BigInteger
import java.math.*;
public class DivideAndRemainderOfBI {
public static void main(String args[]) {
// Initialize two variables divi, divisr
String divi = "120";
String divisr = "4";
// Initialize two BigInteger objects
BigInteger b_int1 = new BigInteger(divi);
BigInteger b_int2 = new BigInteger(divisr);
// Display b_int1 and b_int2
System.out.println("b_int1: " + b_int1);
System.out.println("b_int2: " + b_int2);
System.out.println("divideAndRemainder(BigInteger): ");
// divides this BigInteger (b_int1) by the
// given BigInteger (b_int2) and return the BigInteger[]
// of two values (Quotient, Remainder)
BigInteger[] div_rem = b_int1.divideAndRemainder(b_int2);
System.out.println("Quotient div_rem[0]: " + div_rem[0]);
System.out.println("Remainder div_rem[1]: " + div_rem[1]);
}
}
Output
输出量
b_int1: 120
b_int2: 4
divideAndRemainder(BigInteger):
Quotient div_rem[0]: 30
Remainder div_rem[1]: 0
翻译自: https://www.includehelp.com/java/biginteger-divideandremainder-method-with-example.aspx