logo

שיטת Java Integer max()

ה מקסימום() היא שיטה של ​​מחלקה Integer under Java חבילת .lang. שיטה זו מחזירה באופן מספרי את הערך המרבי בין שני ארגומנטים של השיטה שצוינו על ידי משתמש. ניתן להעמיס על שיטה זו והיא לוקחת את הארגומנטים ב-int, double, float ו-long. שיטה זו מוגדרת על ידי מתמטיקה מעמד.

הערה: אם מספר חיובי ושלילי מועבר כארגומנט, הוא יצר תוצאה חיובית. ואם שני הפרמטרים הועברו כמספר שלילי, זה יוצר תוצאה בגודל נמוך יותר.

תחביר:

להלן ההצהרה של מקסימום() שיטה:

 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) 

פָּרָמֶטֶר:

סוג מידע פָּרָמֶטֶר תיאור חובה/אופציונלי
int א ערך מספרי שהוזן על ידי משתמש. נדרש
int ב ערך מספרי שהוזן על ידי משתמש. נדרש

החזרות:

ה מקסימום() שיטה מחזירה את הערך הגדול יותר בין שני ארגומנטים של השיטה שצוינו על ידי משתמש.

חריגים:

זֶה

גרסת תאימות:

Java 1.5 ומעלה

דוגמה 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)); } } 
בדוק את זה עכשיו

תְפוּקָה:

 Math.max(5485,3242)=5485 

דוגמה 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)); } } 

תְפוּקָה:

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

דוגמה 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)); } } 
בדוק את זה עכשיו

תְפוּקָה:

 Result: -23 

דוגמה 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)); } } 
בדוק את זה עכשיו

תְפוּקָה:

 Result: 23