介绍
在本实验中,你将学习 Java 中 Integer
类的 min()
方法。该方法用于返回作为参数传递的两个数字中数值较小的值(最小值)。
在本实验中,你将学习 Java 中 Integer
类的 min()
方法。该方法用于返回作为参数传递的两个数字中数值较小的值(最小值)。
在你的 Ubuntu 系统中打开终端,并使用以下命令创建一个 Java 项目文件夹:
mkdir project
使用以下命令进入项目文件夹:
cd project/
使用以下命令创建一个 Java 代码文件:
touch IntegerMin.java
min()
方法min()
方法是 Integer
类的一个静态方法。该方法用于返回作为参数传递的两个数字中数值较小的值(最小值)。如果传递了一个正数和一个负数,则返回负数值;但如果传递的两个数字都是负数,则返回绝对值较大的值。
min()
方法的语法如下:
public static int min(int a, int b)
在上述语法中,a
和 b
是需要比较值的参数。
min()
方法将以下 Java 代码复制并粘贴到 IntegerMin.java
文件中。
import java.util.Scanner;
public class IntegerMin {
public static void main(String[] args) {
try {
System.out.println("Enter two integer 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: " + Integer.min(a, b));
} catch(Exception e) {
System.out.println("Exception occurred...");
}
}
}
在上述代码中,我们从用户那里获取两个整数值作为输入,然后使用 min()
方法进行比较。较小的值将作为输出打印出来。
要编译 IntegerMin.java
文件,请打开终端并运行以下命令:
javac IntegerMin.java
要运行程序,请执行以下命令:
java IntegerMin
使用命令行文本编辑器(如 nano
)打开 IntegerMin.java
文件,并将现有代码替换为以下代码块:
import java.lang.Integer;
import java.util.Scanner;
public class IntegerMin {
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("Smaller value is " + Integer.min(x, y));
// print the smaller number between y and z.
System.out.println("Smaller value is " + Integer.min(y, z));
}
}
保存并关闭文件。
再次使用以下命令编译并运行程序:
javac IntegerMin.java
java IntegerMin
运行上述命令后,程序将显示两个整数 x
和 y
之间的较小值,然后显示两个整数 y
和 z
之间的较小值。
使用命令行文本编辑器(如 nano
)打开 IntegerMin.java
文件,并将现有代码替换为以下代码块:
import java.util.Scanner;
public class IntegerMin {
public static void main(String[] args) {
try {
System.out.println("Enter two integer 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: " + Integer.min(a, b));
} catch(Exception e) {
System.out.println("Exception occurred...");
}
}
}
保存并关闭文件。
再次使用以下命令编译并运行程序:
javac IntegerMin.java
java IntegerMin
运行上述命令后,程序将提示输入两个整数值。输入两个数值并按回车键,程序将显示这两个整数之间的较小值。
使用命令行文本编辑器(如 nano
)打开 IntegerMin.java
文件,并将现有代码替换为以下代码块:
import java.util.Scanner;
public class IntegerMin {
public static void main(String[] args) {
try {
System.out.println("Enter two integer 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: " + Integer.min(a, b));
} catch(Exception e) {
System.out.println("Exception occurred...");
}
}
}
保存并关闭文件。
再次使用以下命令编译并运行程序:
javac IntegerMin.java
java IntegerMin
运行上述命令后,程序将提示输入两个整数值。输入两个整数值并按回车键,程序将显示这两个整数之间的较小值。
使用命令行文本编辑器(如 nano
)打开 IntegerMin.java
文件,并将现有代码替换为以下代码块:
import java.util.Scanner;
public class IntegerMin {
public static void main(String[] args) {
try {
int[] array = new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("Enter ten integer values: ");
for(int i=0; i<10; i++) {
array[i] = sc.nextInt();
}
int min = array[0];
for(int j=0; j<10; j++) {
if(min > array[j]) {
min = array[j];
}
}
System.out.println("Smaller value: " + min);
} catch(Exception e) {
System.out.println("Exception occurred...");
}
}
}
保存并关闭文件。
再次使用以下命令编译并运行程序:
javac IntegerMin.java
java IntegerMin
运行上述命令后,程序将提示输入十个整数值。输入十个整数值并按回车键,程序将显示输入的十个整数中的最小值。
在本实验中,你学习了以下内容:
min()
方法Integer
类的 min()
方法的语法和用法min()
方法