Guava IntMath類的mod(int x,int m)方法接受兩個參數x和m,並用於計算m下的x模量值。
用法:
public static int mod(int x, int m)
參數:此方法接受兩個參數x和m,它們是整數類型,並計算x模m。
返回值:該方法返回x mod m,該值將是小於m的非負值。
異常:如果m <= 0,則方法mod(int x,int m)引發ArithmeticException。
以下示例說明了mod(int x,int m)方法:
範例1:
// Java code to show implementation of
// mod(int x, int m) method of Guava's
// IntMath class
import java.math.RoundingMode;
import com.google.common.math.IntMath;
class GFG {
// Driver code
public static void main(String args[])
{
int x1 = -84;
int m1 = 5;
int ans1 = IntMath.mod(x1, m1);
// Using mod(int x, int m)
// method of Guava's IntMath class
System.out.println(x1 + " mod " + m1 + " is:" + ans1);
int x2 = 14;
int m2 = 6;
int ans2 = IntMath.mod(x2, m2);
// Using mod(int x, int m)
// method of Guava's IntMath class
System.out.println(x2 + " mod " + m2 + " is:" + ans2);
}
}
輸出:
-84 mod 5 is:1 14 mod 6 is:2
範例2:
// Java code to show implementation of
// mod(int x, int m) method of Guava's
// IntMath class
import java.math.RoundingMode;
import com.google.common.math.IntMath;
class GFG {
static int findMod(int x, int m)
{
try {
// Using mod(int x, int m)
// method of Guava's IntMath class
// This should throw "ArithmeticException"
// as m <= 0
int ans = IntMath.mod(x, m);
// Return the answer
return ans;
}
catch (Exception e) {
System.out.println(e);
return -1;
}
}
// Driver code
public static void main(String args[])
{
int x = 14;
int m = -3;
try {
// Function calling
findMod(x, m);
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
java.lang.ArithmeticException:Modulus -3 must be > 0
相關用法
- Java Guava IntMath gcd(int a, int b)用法及代碼示例
- Java Guava IntMath pow(int b, int k)用法及代碼示例
- Java Guava IntMath mean()用法及代碼示例
- Java Guava IntMath binomial()用法及代碼示例
- Java Guava IntMath isPowerOfTwo()用法及代碼示例
- Java Guava IntMath floorPowerOfTwo()用法及代碼示例
- Java Guava IntMath isPrime()用法及代碼示例
- Java Guava IntMath ceilingPowerOfTwo()用法及代碼示例
- Java Guava IntMath sqrt(int x, RoundingMode mode)用法及代碼示例
- Java IntMath.checkedSubtract(int a, int b)用法及代碼示例
- Java IntMath.checkedAdd(int a, int b)用法及代碼示例
- Java IntMath.checkedMultiply(int a, int b)用法及代碼示例
- Java IntMath.checkedPow(int b, int k)用法及代碼示例
注:本文由純淨天空篩選整理自bansal_rtk_大神的英文原創作品 Java Guava | mod() method of IntMath Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。