English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java Math mathematical methods
La méthode Java Math min() retourne la valeur minimale parmi les paramètres spécifiés.
La syntaxe de la méthode min() est :
Math.min(arg1, arg2)
Attention:min() est une méthode statique. Par conséquent, nous pouvons utiliser le nom de la classe Math pour accéder à cette méthode.
arg1 / arg2 - Paramètres retournant la valeur minimale
RemarqueRemarque : les types de données des paramètres doivent être int, long, float ou double.
Retourne la valeur minimale parmi les paramètres spécifiés
class Main { public static void main(String[] args) { //Math.min() avec un paramètre int int num1 = 35; int num2 = 88; System.out.println(Math.min(num1, num2}); // 35 /Avec un paramètre long/Math.min() long num3 = 64532L; long num4 = 252324L; System.out.println(Math.min(num3, num4}); // 64532 //Math.min() avec un paramètre float float num5 = 4.5f; float num6 = 9.67f; System.out.println(Math.min(num5, num6}); // 4.5 //Math.min() avec un paramètre double double num7 = 23.44d; double num8 = 32.11d; System.out.println(Math.min(num7, num8}); // 23.44 } }
Dans cet exemple, nous utilisons la méthode Math.min() avec des paramètres de types int, long, Float et Double.
class Main { public static void main(String[] args) { //Créer un tableau de type int int[] arr = {4, 2, 5, 3, 6}; //Assign the first element of the array as the minimum value int min = arr[0]; for (int i = 1; i < arr.length; i++) { //Compare all elements with min //Assign the minimum value to min min = Math.min(min, arr[i]); } System.out.println("Minimum value: " + min); } }
In the above example, we created an array named arrArray. Initially, the variable min stores the first element of the array.
Here, we use a for loop to access all elements of the array. Note this line,
min = Math.min(min, arr[i])
The Math.min() method compares the variable min with all elements of the array and assigns the minimum value to min.