Java Integer max() metoda
The max() je metoda klase Integer pod Java .lang paket. Ova metoda numerički vraća maksimalnu vrijednost između dva argumenta metode koje je odredio korisnik. Ova metoda može biti preopterećena i uzima argumente u int, double, float i long. Ovu metodu specificira matematika Klasa.
Napomena: Ako se pozitivni i negativni broj proslijede kao argument, generira se pozitivan rezultat. A ako su oba parametra proslijeđena kao negativan broj, generira se rezultat s nižom veličinom.
Sintaksa:
Slijedi izjava od max() metoda:
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)
Parametar:
| Tip podataka | Parametar | Opis | Obavezno/opcionalno |
|---|---|---|---|
| int | a | Brojčana vrijednost koju je unio korisnik. | Potreban |
| int | b | Brojčana vrijednost koju je unio korisnik. | Potreban |
Povratak:
The max() metoda vraća veću vrijednost između dva argumenta metode koje je naveo korisnik.
Iznimke:
DA
Verzija kompatibilnosti:
Java 1.5 i novije
Primjer 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)); } } Testirajte sada Izlaz:
Math.max(5485,3242)=5485
Primjer 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)); } } Izlaz:
Enter the Two Numeric value: 45 77 Larger value of Math.max(45,77) = 77
Primjer 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)); } } Testirajte sada Izlaz:
Result: -23
Primjer 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)); } } Testirajte sada Izlaz:
Result: 23