Java中的 StrictMath max() 方法及示例
Java.lang.StrictMath.max() 方法返回两个值中较大的一个。此方法有四种变体,传递了不同类型的参数。
所有这些都在下面讨论:
- max(double num1, double num2)是用于获取两个给定双精度值参数的最大值的内置方法。当num1和num2具有相同的值时,它返回相同的值。当任一值为 NaN 时返回 NaN,则结果为 .它占用负零严格小于正零。当一个参数为正零而另一个为负零时,它返回正零。
句法 :
public static double max(double num1, double num2)
参数:该方法接受两个参数:
- 表示参数的 double 类型的num1
- 代表另一个参数的 double 类型的num2
返回值:该方法返回num1和num2中的较大者。
例子 :
Input: num1 = 8 nm2 = 19 Output: 19.0
下面的程序说明了Java.lang.StrictMath.max() 方法。
方案一:// Java praogram to illustrate the // Java.lang.StrictMath.max() import java.lang.*; public class Geeks { public static void main(String[] args) { double num1 = 117, num2 = 711; double max_Value = StrictMath.max(num1, num2); System.out.println("max of the two num is " + max_Value); } }
输出:max of the two num is 711.0
方案二:
// Java praogram to illustrate the // Java.lang.StrictMath.max() import java.lang.*; public class Geeks { public static void main(String[] args) { double num1 = -87, num2 = -11; double max_Value = StrictMath.max(num1, num2); System.out.println("max of the two num is " + max_Value); } }
输出:max of the two num is -11.0
错误条件示例:
// Java praogram to illustrate the // error condition in // Java.lang.StrictMath.max() Method import java.lang.*; public class Geeks { public static void main(String[] args) { double num1 = 51, num2 = 71, num3 = 3, num4 = -93, num5 = -93; double a = 0.0; num1 = a / 0.0; double max_Value = StrictMath.max(num1, a); System.out.println("max of the two num is " + max_Value); } }
输出:
max of the two num is NaN
- max(float num1, float num2)是内置方法,用于获取两个给定浮点值参数的最大值。当num1和num2具有相同的值时,它返回相同的值。当任一值为 NaN 时,它返回 NaN。它占用负零严格小于正零。当一个参数为正零而另一个为负零时,它返回正零。
句法 :
public static float max(float num1, float num2)
参数:该方法接受两个参数:
- 代表参数的浮点型num1
- 表示另一个参数的浮点类型的num2
返回值:该方法返回num1和num2中的较大者。
例子 :
Input: num1 = 87 nm2 = 59 Output: 87.0
下面的程序说明了Java.lang.StrictMath.max() 方法。
方案一:// Java praogram to illustrate the // Java.lang.StrictMath.max() import java.lang.*; public class Geeks { public static void main(String[] args) { float num1 = 75, num2 = 81, num3 = -62, num4 = -62, num5 = -92; float max_Value = StrictMath.max(num1, num2); System.out.println("max of the two num is " + max_Value); float max_Value1 = StrictMath.max(num3, num4); System.out.println("max of the two num is " + max_Value1); float max_Value2 = StrictMath.max(num4, num5); System.out.println("max of the two num is " + max_Value2); } }
输出:max of the two num is 81.0 max of the two num is -62.0 max of the two num is -62.0
- max(int num1, int num2)是用于获取两个给定 int 值参数的最大值的内置方法。当num1和num2具有相同的值时,它返回相同的值。当任一值为 NaN 时,它返回 NaN。当一个参数为正零而另一个为负零时,它返回正零。简而言之,它返回更接近 Integer.MAX_VALUE 值的参数。
句法 :public static int max(int num1, int num2)
参数:该方法接受两个参数:
- num1表示参数的 int 类型
- int 类型的num2表示另一个参数
返回值:该方法返回num1和num2中的较大者。
例子 :
Input: num1 = 13 nm2 = 19 Output: 19.0
下面的程序说明了Java.lang.StrictMath.max() 方法。
方案一:// Java praogram to illustrate the // Java.lang.StrictMath.max() import java.lang.*; public class Geeks { public static void main(String[] args) { int num1 = 51, num2 = 72, num3 = -31, num4 = -31, num5 = -89; int max_Value = StrictMath.max(num1, num2); System.out.println("max of the two num is " + max_Value); int max_Value1 = StrictMath.max(num3, num4); System.out.println("max of the two num is " + max_Value1); int max_Value2 = StrictMath.max(num4, num5); System.out.println("max of the two num is " + max_Value2); } }
输出:max of the two num is 72 max of the two num is -31 max of the two num is -31
- max(long num1, long num2)是用于获取两个给定长值参数的最大值的内置方法。当num1和num2具有相同的值时,它返回相同的值。当任一值为 NaN 时,它返回 NaN。简而言之,它返回更接近 Long.MAX_VALUE 值的参数。
句法 :public static long max(long num1, long num2)
参数:该方法接受两个参数:
- 表示参数的long类型的num1
- 表示另一个参数的 long 类型的num2
返回值:该方法返回num1和num2中的较大者。
例子 :
Input: num1 = 78772728 nm2 = 1617177 Output: 78772728.0
下面的程序说明了Java.lang.StrictMath.max() 方法。
方案一:// Java praogram to illustrate the // Java.lang.StrictMath.max() import java.lang.*; public class Geeks { public static void main(String[] args) { long num1 = 87287, num2 = 787822, num3 = -3271, num4 = -3271, num5 = -459; long max_Value = StrictMath.max(num1, num2); System.out.println("max of the two num is " + max_Value); long max_Value1 = StrictMath.max(num3, num4); System.out.println("max of the two num is " + max_Value1); long max_Value2 = StrictMath.max(num4, num5); System.out.println("max of the two num is " + max_Value2); } }
输出:max of the two num is 787822 max of the two num is -3271 max of the two num is -459