目录
Java max() 语法
实现 Java max() 函数的示例
示例#4
Example #5
Conclusion
首页 Java java教程 Java 最大()

Java 最大()

Aug 30, 2024 pm 03:38 PM
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);
登录后复制
注意:如果参数以正数和负数传递,则函数返回正结果。如果两个参数都作为负数传递,则该函数返回较低量值的结果。如果参数作为相同的值传递,则函数返回相同的结果。如果任一参数作为 NAN 值传递,则该函数将返回 NAN 作为结果。

实现 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+"." );
}
}
登录后复制

输出:

Java 最大()

说明:

如上面的代码,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+"." );
}
}
登录后复制

输出:

Java 最大()

说明:

如上面的代码,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+"." );
}
}
登录后复制

输出:

Java 最大()

如上面的代码所示,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+"." );
}
}
登录后复制

输出:

Java 最大()

如上面的代码所示,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 :

Java 最大()

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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Java 中的平方根 Java 中的平方根 Aug 30, 2024 pm 04:26 PM

Java 中的平方根

Java 中的完美数 Java 中的完美数 Aug 30, 2024 pm 04:28 PM

Java 中的完美数

Java 中的随机数生成器 Java 中的随机数生成器 Aug 30, 2024 pm 04:27 PM

Java 中的随机数生成器

Java中的Weka Java中的Weka Aug 30, 2024 pm 04:28 PM

Java中的Weka

Java 中的阿姆斯特朗数 Java 中的阿姆斯特朗数 Aug 30, 2024 pm 04:26 PM

Java 中的阿姆斯特朗数

Java 中的史密斯数 Java 中的史密斯数 Aug 30, 2024 pm 04:28 PM

Java 中的史密斯数

Java Spring 面试题 Java Spring 面试题 Aug 30, 2024 pm 04:29 PM

Java Spring 面试题

突破或从Java 8流返回? 突破或从Java 8流返回? Feb 07, 2025 pm 12:09 PM

突破或从Java 8流返回?

See all articles