使用Java中的Math.max获取两个数的最大值
为了在Java中使用Math.max获得两个数字的最大值,我们使用java.lang.Math.max()方法。Math.max()接受两个数字,并返回两个中较大的一个。结果在数字线上接近正无穷大。即使其中一个值不是数字(NaN),结果也是NaN。
声明-java.lang.Math.max()方法声明如下-
public static int max(int a, int b) public static double max(double a, double b) public static long max(long a, long b) public static float max(float a, float b)
让我们看一个程序,使用Math.max()方法获取两个数的最大值
示例
import java.lang.Math;
public class Example {
public static void main(String[] args) {
//声明和初始化一些整数值
int a = 10;
int b = 9;
//声明和初始化一些浮点值
float c = 10.00f;
float d = 9.99f;
//声明并初始化一些double值
double x = 300.01d;
double y = 290.344d;
//声明并初始化一些长值
long r = 123456l;
long s = 35678l;
System.out.println("Maximum of " + a +" and " + b +" is " + Math.max(a,b));
System.out.println("Maximum of " + c +" and " + d +" is " + Math.max(c,d));
System.out.println("Maximum of " + x +" and " + y +" is " + Math.max(x,y));
System.out.println("Maximum of " + r +" and " + s +" is " + Math.max(r,s));
}
}输出结果
Maximum of 10 and 9 is 10 Maximum of 10.0 and 9.99 is 10.0 Maximum of 300.01 and 290.344 is 300.01 Maximum of 123456 and 35678 is 123456
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短