Java 最大()
java max() 函数用于返回两个数值中的最大值。 java max()函数是java中的内置函数,它定义在Java.lang.math类中,因此要在程序中使用max()函数,必须导入Java.lang.math类。 max() 函数接受两个数值数据类型的参数,并返回两个数值参数中的最大值;它可以针对 int、float、double 和 long 等数据类型的不同参数进行重载。它不会抛出任何异常。
广告 该类别中的热门课程 3DS MAX 架构 - 专业化 | 4 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
Java max() 语法
以下是语法:
语法:
data_Type max(data_Type x, data_Type y)
数据类型可以是 int、float、double 和 long。
以下是 max() 函数的参数:
x和y是两个数值参数,要返回其中最大的数字。
返回值:函数的返回值最多为两个数字,与传入参数的data_type相同。
java中的max()函数可以针对不同数据类型的参数进行重载,因此以下是java中max()函数的语法:
public static int max(int x, int y);
public static float max(float x, float y) ;
public static long max( long x, long y );
public static double max(double x, double y);
实现 Java max() 函数的示例
以下是示例:
示例#1
我们编写java代码是为了更清楚地理解max()函数,下面的示例中我们使用max()函数来查找用户传递的两个整数中的最大值,如下所示。
代码:
import java.lang.Math; class Demo { public static void main( String[] arg ) { // variable declaration int x = 1024, y = 2034, result; // calling max() built in function which is define in Math built in class result = Math.max(x,y); System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." ); } }
输出:
说明:
如上面的代码,x 和 y 是两个变量,分别声明并初始化为 1024 和 2034 个值,稍后,将这些变量作为参数调用 max() 函数 (int max(int x, int y )重载)因此该函数的结果是 2034,这是 1024 和 2034 中的最大数字。
示例#2
我们编写java代码来理解max()函数,其中我们使用max()函数来查找用户传递的两个双精度数中的最大值,如下所示。
代码:
import java.lang.Math; class Demo { public static void main( String[] arg ) { // variable declaration double x = 134.78, y = 56.89, result; // calling max() built in function which is define in Math built in class result = Math.max(x,y); System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." ); } }
输出:
说明:
如上面的代码,x和y是两个变量,分别声明并初始化为134.78和56.89 double值,随后,调用max()函数并将这些变量作为参数传递(double max(double x, double y) 重载) 因此该函数的结果是 134.78,这是 1024 和 2034 中的最大数字。
示例#3
我们编写java代码来理解max()函数,其中我们使用max()函数来查找用户从控制台接受的两个整数中的最大值。
代码:
import java.lang.Math; import java.util.Scanner; class Demo { public static void main( String[] arg ) { // variable declaration int x, y, result; // accept two numbers of integer type from console System.out.println( "Enter the Two Numeric value: "); Scanner sc = new Scanner(System.in); x = sc.nextInt(); y = sc.nextInt(); result = Math.max(x,y); //Print the maximum number between x and y System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." ); } }
输出:
如上面的代码所示,x 和 y 是两个变量,它们通过扫描仪对象声明并接受用户的这些变量值。随后,调用 max() 函数并将这些变量作为参数传递,因此该函数基于用户输入的值(例如 10 和 20)的结果是 20。
示例#4
我们为 max() 函数编写 java 代码,其中使用 max() 函数传递两个负值并找到最大值,如下所示。
代码:
import java.lang.Math; import java.util.Scanner; class Demo { public static void main( String[] arg ) { // variable declaration int x, y, result; x = -10; y = -20; result = Math.max(x,y); //Print the maximum number between x and y of lower magnitude System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." ); } }
输出:
如上面的代码所示,x 和 y 是两个变量,分别声明并初始化为负值 -10 和 -20 值,稍后,调用 max() 函数并将这些变量作为参数传递,因此结果为该函数为 -10,其幅度较低。
Example #5
We write the java code for max() function where we use the max() function to pass positive and negative values and find the maximum, as below.
Code:
import java.lang.Math; import java.util.Scanner; class Demo { public static void main( String[] arg ) { // variable declaration int x, y, result; x = 10; y = -20; result = Math.max(x,y); //Print the maximum number between x and y System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." ); } }
Output :
As in the above code, the x and y are two variables declare and initialize with values 10 and -20 values respectively, and the result of this function is 10.
Conclusion
The java max() function which is a built-in function in Java.lang.math class is used to gets the maximum of two numerical values, as we have seen above with an example.
以上是Java 最大()的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)