Guava的IntMath類的mean(int x,int y)方法接受兩個參數x和y,並計算它們的算術平均值,取整為負無窮大。此方法具有溢出彈性。
用法:
public static int mean(int x, int y)
參數:此方法接受兩個參數x和y,它們是整數類型。
返回值:該方法返回x和y的算術平均值,四舍五入為負無窮大。
異常:該方法沒有任何異常。
範例1:
// Java code to show implementation of
// mean(int x, int y) 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 x = 1542;
int y = 421;
// Using mean(int x, int y)
// method of Guava's IntMath class
int ans = IntMath.mean(x, y);
// Displaying the result
System.out.println("Mean of " + x + " and "
+ y + " is:" + ans);
}
}
輸出:
Mean of 1542 and 421 is:981
範例2:
// Java code to show implementation of
// mean(int x, int y) 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 x = 214;
int y = 154;
// Using mean(int x, int y)
// method of Guava's IntMath class
int ans = IntMath.mean(x, y);
// Displaying the result
System.out.println("Mean of " + x + " and "
+ y + " is:" + ans);
}
}
輸出:
Mean of 214 and 154 is:184
相關用法
- Java Guava IntMath gcd(int a, int b)用法及代碼示例
- Java Guava IntMath pow(int b, int k)用法及代碼示例
- Java Guava IntMath mod()用法及代碼示例
- 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 | mean() method of IntMath Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。