Guava LongMath類的mean(long x,long y)方法接受兩個參數x和y,並計算它們的算術平均值,取整為負無窮大。此方法具有溢出彈性。
用法:
public static long mean(long x, long y)
參數:此方法接受長類型的兩個參數x和y。
返回值:該方法返回x和y的算術平均值,四舍五入為負無窮大。
異常:該方法沒有任何異常。
範例1:
// Java code to show implementation of
// mean(long x, long y) method of Guava's
// LongMath class
import java.math.RoundingMode;
import com.google.common.math.LongMath;
class GFG {
// Driver code
public static void main(String args[])
{
long x = 1542;
long y = 421;
// Using mean(long x, long y)
// method of Guava's LongMath class
long ans = LongMath.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(long x, long y) method of Guava's
// LongMath class
import java.math.RoundingMode;
import com.google.common.math.LongMath;
class GFG {
// Driver code
public static void main(String args[])
{
long x = 65;
long y = 41;
// Using mean(long x, long y)
// method of Guava's LongMath class
long ans = LongMath.mean(x, y);
// Displaying the result
System.out.println("Mean of " + x + " and "
+ y + " is:" + ans);
}
}
輸出:
Mean of 65 and 41 is:53
相關用法
- Java Guava LongMath gcd(long a, long b)用法及代碼示例
- Java Guava LongMath mod(long x, long m)用法及代碼示例
- Java Guava LongMath pow(long b, int k)用法及代碼示例
- Java Guava LongMath isPowerOfTwo(long x)用法及代碼示例
- Java Guava LongMath sqrt(long x, RoundingMode mode)用法及代碼示例
- Java LongMath.divide(long, long, RoundingMode)用法及代碼示例
- Java LongMath.checkedAdd(long a, long b)用法及代碼示例
- Java LongMath.checkedPow(long b, int k)用法及代碼示例
- Java LongMath log10(long x, RoundingMode mode)用法及代碼示例
- Java Guava LongMath binomial(int n, int k)用法及代碼示例
- Java LongMath.checkedMultiply(int a, int b)用法及代碼示例
- Java Longs.checkedSubtract(long a, long b)用法及代碼示例
- Java Longs.indexOf(long[] array, long[] target)用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Java Guava | mean(long x, long y) of LongMath Class with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。