首页 > Java > java教程 > 正文

java min()

WBOY
发布: 2024-08-30 15:38:48
原创
539 人浏览过

在Java中,min()是一个内置方法,它返回两个数字的最小值。它继承自 java.lang.math 包,参数采用 double、int、long 和 float 类型。而且这个方法是可以重载的,实现这个方法是有一定条件的。它将在解释工作的部分中进行讨论。除此之外,min() 方法的语法和示例可以在下面的部分中看到。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

正如已经讨论过的,此方法中可以使用不同的数据类型,例如 int、float、double 和 long。以下是方法 min() 的这些不同数据类型对应的语法。

public static int min(int num1, int num2)  //syntax of min with datatype int
登录后复制
public static long min(long num1, long num2)  //syntax of min with datatype long
登录后复制
public static float min(float num1, float num2)  //syntax of min with datatype float
登录后复制
public static double min(double num1, double num2)  //syntax of min with double
登录后复制

参数:不同数据类型的num1和num2,返回其中的最小值。

返回值:将返回作为参数传递的至少两个数字,并且结果的数据类型将与参数相同。

Java 中的 min() 方法是如何工作的?

1.如果将负数和正数作为方法的参数传递,则生成的结果将为负数。

示例:如果给出数字 -32 和 21 作为参数,则将返回 -32。

2. 如果作为方法参数传递的两个参数均为负数,则生成的结果将是具有较高量级的结果。也就是说,它将更接近-ve(负)无穷大。

示例:如果给出数字 -32 和 -21 作为参数,则将返回 -32。

3. 如果作为方法参数传递的两个参数相同,则生成的结果将是相同的值。

示例:如果给出数字 -32 和 -32 作为参数,则将返回 -32。

4.如果 NaN(Not a Number) 是任一值,生成的结果也将为 NaN。

实现 Java min() 方法的示例

下面是 Java min() 方法的示例:

示例#1

求两个int型正数中最小值的Java程序。

代码:

public class MinExample {
public static void main(String[] args) {
// Declare two numbers of <u>int</u> type
int x = 41;
int y = 67;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
登录后复制

输出:

java min()

在此程序中,声明了两个正数,41 和 67,并使用 min() 方法找到其中的最小值 41。

示例#2

Java 程序求两个 int 类型数字中的最小值,其中一个为正,另一个为负。

代码:

public class MinExample {
public static void main(String[] args) {
// Declare two numbers of <u>int</u> type
int x = 41;
int y = -67;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
登录后复制

输出:

java min()

在这个程序中,声明了一个正数41和一个负数-67。其中最小值 -67,更接近负无穷大,可以使用 min() 方法找到。

示例#3

求两个int型负数中最小值的Java程序。

代码:

public class MinExample {
public static void main(String[] args) {
// Declare two numbers of <u>int</u> type
int x = -41;
int y = -67;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
登录后复制

输出:

java min()

在此程序中,声明了两个负数,-41 和 -67。其中最小值 -67,更接近负无穷大,可以使用 min() 方法找到。

示例#4

求两个双精度型正数中最小值的Java程序。

代码:

public class MinExample {
public static void main(String[] args) {
// Declare two numbers of double type
double x = 26.11;
double y = 26.12;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
登录后复制

输出:

java min()

与上面的程序不同,这里声明了两个双精度类型的正数,26.11和26.12。但是,使用类似于上述程序的 min() 方法可以找到其中的最小值 26.11。

示例#5

求两个float类型正数中最小值的Java程序。

代码:

public class MinExample {
public static void main(String[] args) {
// Declare two numbers of float type
float x = 26.11f;
float y = 26.12f;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
登录后复制

输出:

java min()

这里声明了两个正数,float类型的26.11f和26.12f。其中最小值 26.11 是使用 min() 方法找到的。

示例#6

用于查找两个用户输入数字中最小值的 Java 程序。

代码:

import java.util.Scanner;
public class MinExample {
public static void main(String[] args) {
System.out.println("Enter two numbers from which the minimum has to be found: ");
//read input numbers from the user
Scanner in= new Scanner(System.in);
//store first number in x
int x = in.nextInt();
//store second number in y
int y = in.nextInt();
in.close();
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
登录后复制

输出:

java min()

在此程序中,要求用户输入两个数字。如您所见,给出的数字为 32 和 57,其中返回 32 作为最小数字。

如果两个数字相同会发生什么?

java min()

可以看到,结果会返回相同的数字。

以上是java min()的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!