Guava – LongMath.mean方法与实例

Guava – LongMath.mean方法与实例

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-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程