Math.max only takes two arguments. If you want the maximum of three, use Math.max(MY_INT1, Math.max(MY_INT2, MY_DOUBLE2)).
CodeGym
codegym.cc โบ java blog โบ java math โบ java math max() method
Java Math.max() method with Examples
December 5, 2024 - Although Math.max allows you to give two values, you can improvise it to find the maximum among three or more values. Check the following example. public class Main { public static void main(String args[]) { int x = 40; int y = 60; int z = 75; ...
DaniWeb
daniweb.com โบ programming โบ software-development โบ threads โบ 172908 โบ max-and-min-of-more-than-2-numbers
java - Max and Min of more than 2 numbers | DaniWeb
I am trying to make a program that finds the max and min of more than 2 inputs and i am trying to use the Math.Min and Math.Max method but i cant seem to get them too work since they only accept 2 parameters. ... Math.min/max only compare two values at a time, so to handle N values you iterate once, carrying the current min and max.
Videos
Top answer 1 of 12
110
Math.max only takes two arguments. If you want the maximum of three, use Math.max(MY_INT1, Math.max(MY_INT2, MY_DOUBLE2)).
2 of 12
31
you can use this:
Collections.max(Arrays.asList(1,2,3,4));
or create a function
public static int max(Integer... vals) {
return Collections.max(Arrays.asList(vals));
}
iO Flood
ioflood.com โบ blog โบ math-max-java
Math.max() Java Function | Usage, Syntax, and Examples
February 29, 2024 - In this example, we use the Math.max() function to find the maximum of the numbers 5 and 10. The function returns 10, which is the larger of the two numbers, and this value is then printed to the console. This is a basic way to use Math.max in Java, but thereโs much more to learn about this function and its applications.
W3Schools
w3schools.com โบ java โบ ref_math_max.asp
Java Math max() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... System.out.println(Math.max(2.0, 0.25)); System.out.println(Math.max(31.2f, 18.0f)); System.out.println(Math.max(14, 22)); System.out.println(Math.max(96L, 2048L));
Codecademy
codecademy.com โบ docs โบ java โบ math methods โบ .max()
Java | Math Methods | .max() | Codecademy
October 22, 2022 - The Math.max() method returns the maximum value from the given two arguments. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn to code in Java โ a robust programming language used to create software, web and mobile apps, and more.
Top answer 1 of 2
9
You could use varargs:
Copypublic static Integer max(Integer... vals) {
Integer ret = null;
for (Integer val : vals) {
if (ret == null || (val != null && val > ret)) {
ret = val;
}
}
return ret;
}
public static void main(String args[]) {
System.out.println(max(1, 2, 3, 4, 0, -1));
}
Alternatively:
Copypublic static int max(int first, int... rest) {
int ret = first;
for (int val : rest) {
ret = Math.max(ret, val);
}
return ret;
}
2 of 2
1
You can use a simple loop:
Copypublic Integer max(final Collection<Integer> ints) {
Integer max = Integer.MIN_VALUE;
for (Integer integer : ints) {
max = Math.max(max, integer);
}
return max;
}
Javatpoint
javatpoint.com โบ java-math-max-method
Java Math.max() method with Examples - Javatpoint
Java Math.max() method with Examples on abs(), min(), max(), avg(), round(), ceil(), floor(), pow(), sqrt(), sin(), cos(), tan(), exp() etc.
Top answer 1 of 3
38
Two things: Change the variables x, y, z as int and call the method as Math.max(Math.max(x,y),z) as it accepts two parameters only.
In Summary, change below:
String x = keyboard.nextLine();
String y = keyboard.nextLine();
String z = keyboard.nextLine();
int max = Math.max(x,y,z);
to
int x = keyboard.nextInt();
int y = keyboard.nextInt();
int z = keyboard.nextInt();
int max = Math.max(Math.max(x,y),z);
2 of 3
2
You should know more about java.lang.Math.max:
java.lang.Math.max(arg1,arg2)only accepts 2 arguments but you are writing 3 arguments in your code.- The 2 arguments should be
double,int,longandfloatbut your are writingStringarguments in Math.max function. You need to parse them in the required type.
You code will produce compile time error because of above mismatches.
Try following updated code, that will solve your purpose:
import java.lang.Math;
import java.util.Scanner;
public class max {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please input 3 integers: ");
int x = Integer.parseInt(keyboard.nextLine());
int y = Integer.parseInt(keyboard.nextLine());
int z = Integer.parseInt(keyboard.nextLine());
int max = Math.max(x,y);
if(max>y){ //suppose x is max then compare x with z to find max number
max = Math.max(x,z);
}
else{ //if y is max then compare y with z to find max number
max = Math.max(y,z);
}
System.out.println("The max of three is: " + max);
}
}
Scaler
scaler.com โบ home โบ topics โบ max function in java
Max Function in Java - Scaler Topics
April 20, 2024 - How can you find the maximum of two real numbers? The first answer is you compare both real numbers using comparison operators like greater-than (>) or less-than (<) operators and find the maximum number.
Reddit
reddit.com โบ r/javahelp โบ can you use the math.max() method for 4 values instead of 2?
r/javahelp on Reddit: Can you use the Math.max() method for 4 values instead of 2?
November 14, 2021 -
I need to compare 4 values and determine which one is biggest.
Tutorialspoint
tutorialspoint.com โบ java โบ lang โบ math_max_int.htm
Java - Math max(int x, int y) Method
The java.lang.Math.max(int a, int b) returns the greater of two int values. That is, the result is the argument closer to positive infinity. If the arguments have the same value, the result is that same value.
Naukri
naukri.com โบ code360 โบ library โบ math-max-in-java
Math.max in Java - Naukri Code 360
Almost there... just a few more seconds
TutorialKart
tutorialkart.com โบ java โบ java-math โบ java-math-max
Java Math.max() - Maximum Value
November 23, 2020 - max() accepts two numbers as arguments, and returns the greater of the two values.
GeeksforGeeks
geeksforgeeks.org โบ java โบ java-math-max-method
Java Math max() Method - GeeksforGeeks
May 14, 2025 - Example 3: In this example, we will see the usage of Math.max() method with two negative numbers. ... // Java program to demonstrate the // use of max() function when two // negative integers are passed as arguments import java.lang.Math; public class Geeks { public static void main(String args[]) { int a = -25; int b = -23; // prints the maximum of two numbers System.out.println(Math.max(a, b)); } }
Programiz
programiz.com โบ java-programming โบ library โบ math โบ max
Java Math max()
Become a certified Java programmer. Try Programiz PRO! ... The max() method returns the maximum value among the specified arguments. class Main { public static void main(String[] args) { // compute max of 88 and 98 System.out.println(Math.max(88, 98)); } } // Output: 98 ... Here, max() is a static method. Hence, we are accessing the method using the class name, Math. The max() method takes two parameters.
Upgrad
upgrad.com โบ home โบ tutorials โบ software & tech โบ the max function in java
Max Function in Java: A Complete Guide with Practice Exercises
March 18, 2025 - The return value is the larger of the two input values. If a > b, it returns a. If b > a, it returns b. If a == b, it returns either a or b (since they are equal). ... Java allows you to find maximum values between multiple numbers by connecting multiple Math.max() operations sequentially.
TutorialsPoint
tutorialspoint.com โบ get-the-maximum-of-two-numbers-using-math-max-in-java
Get the maximum of two numbers using Math.max in Java
import java.lang.Math; public class Example { public static void main(String[] args) { // declaring and intializing some integer values int a = 10; int b = 9; // declaring and intializing some float values float c = 10.00f; float d = 9.99f; // declaring and initializing some double values double x = 300.01d; double y = 290.344d; // declaring and initializing some long values long r = 123456l; long s = 35678l; System.out.println("Maximum of " + a +" and " + b +" is " + Math.max(a,b)); System.out.println("Maximum of " + c +" and " + d +" is " + Math.max(c,d)); System.out.println("Maximum of " + x +" and " + y +" is " + Math.max(x,y)); System.out.println("Maximum of " + r +" and " + s +" is " + Math.max(r,s)); } }
Tutorialspoint
tutorialspoint.com โบ home โบ java/lang โบ java math.max() method
Understanding Java Math.max() Method
September 1, 2008 - The Java Math max(double a, double b) returns the greater of two double values. That is, the result is the argument closer to positive infinity. If the arguments have the same value, the result is that same value.