使用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 儿童毕业祝福语 简短6
14 开工仪式横幅祝福语简短
15 孩子日常祝福语大全简短
16 清晨寄语诗句祝福语简短
17 结婚送亲认亲祝福语简短
18 虎年喝酒拜年祝福语简短