使用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 哥哥结婚的祝福语简短