logo

Mètode Java Integer max()

El màxim () és un mètode de classe Integer sota Java paquet .lang. Aquest mètode retorna numèricament el valor màxim entre els dos arguments del mètode especificats per un usuari. Aquest mètode es pot sobrecarregar i pren els arguments en int, double, float i long. Aquest mètode està especificat per Matemàtiques Classe.

Nota: si es passa un nombre positiu i un negatiu com a argument, es genera un resultat positiu. I si, tots dos paràmetres passen com a nombre negatiu, es genera un resultat amb la magnitud inferior.

Sintaxi:

A continuació es presenta la declaració de màxim () mètode:

 public static int max(int a, int b) public static long max(long a, long b) public static float max(float a, float b) public static double max(double a, double b) 

Paràmetre:

DataType Paràmetre Descripció Obligatori/Opcional
int a Valor numèric introduït per un usuari. Obligatori
int b Valor numèric introduït per un usuari. Obligatori

Devolucions:

El màxim () El mètode retorna el valor més gran entre els dos arguments del mètode especificats per un usuari.

Excepcions:

AIXÒ

Versió de compatibilitat:

Java 1.5 i superior

Exemple 1

 public class IntegerMaxExample1 { public static void main(String[] args) { // get two integer numbers int x = 5485; int y = 3242; // print the larger number between x and y System.out.println('Math.max(' + x + ',' + y + ')=' + Math.max(x, y)); } } 
Prova-ho ara

Sortida:

 Math.max(5485,3242)=5485 

Exemple 2

 import java.util.Scanner; public class IntegerMaxExample2 { public static void main(String[] args) { //Get two integer numbers from console System.out.println('Enter the Two Numeric value: '); Scanner readInput= new Scanner(System.in); int a = readInput.nextInt(); int b = readInput.nextInt(); readInput.close(); //Print the larger number between a and b System.out.println('Larger value of Math.max(' + a + ',' + b + ') = ' + Math.max(a, b)); } } 

Sortida:

 Enter the Two Numeric value: 45 77 Larger value of Math.max(45,77) = 77 

Exemple 3

 public class IntegerMaxExample3 { public static void main(String[] args) { //Get two integer numbers int a = -25; int b = -23; // Prints result with lower magnitude System.out.println('Result: '+Math.max(a, b)); } } 
Prova-ho ara

Sortida:

 Result: -23 

Exemple 4

 public class IntegerMaxExample4 { public static void main(String[] args) { //Get two integer numbers int a = -75; int b = 23; // Prints result with positive value System.out.println('Result: '+Math.max(a, b)); } } 
Prova-ho ara

Sortida:

 Result: 23