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


Java Guava IntMath mean()用法及代碼示例


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

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



相關用法


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