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


Java Guava LongMath mean(long x, long y)用法及代碼示例


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

參考: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/LongMath.html#mean-long-long-



相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Java Guava | mean(long x, long y) of LongMath Class with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。