Java Integer.min()
方法
原文:https://www.studytonight.com/java-wrapper-class/java-integer-min-method
Java min()
方法是java.lang
包的Integer
类的一部分,由Math
类的Math.min()
方法指定。此方法用于返回作为参数传递的两个数字中数值较小的值(最小值)。
如果传递了一个正数和一个负数,则返回负值,但是如果传递的两个数字都是负数,则返回更大的数值。
语法:
public static int min(int a, int b)
参数:
传递的参数是用户用来检查传递的两个数字的最小值的数值。
返回:
作为参数传递的两个值中数值较低的值。
例 1:
这里,取一个正数和一个负数,因此返回负值,如果两者都是负数,则返回更大的值。
import java.lang.Integer;
public class StudyTonight
{
public static void main(String[] args)
{
int x = 5485;
int y = -3242;
int z = -5645;
// print the smaller number between x and y
System.out.println("Lower number is " + Integer.min(x, y));
// print the smaller number between y and z
System.out.println("Lower number is " + Integer.min(y, z));
}
}
下数为-3242 下数为-5645
例 2:
这里有一个用户定义的例子,任何使用这段代码的人都可以输入自己选择的值,并获得等效的输出。
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.println("Enter the Two values: ");
Scanner sc= new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
//Print the smaller number between a and b
System.out.println("Smaller value is " + Integer.min(a, b));
}
catch(Exception e)
{
System.out.println("Exception occurred...");
}
}
}
输入两个值: 78 -98 较小的值为-98
- T4】输入两个值: 45-65 较小的值为-65
实时示例:
在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。