Math.max only takes two arguments. If you want the maximum of three, use Math.max(MY_INT1, Math.max(MY_INT2, MY_DOUBLE2)).
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));
}
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);
}
}
Videos
07:34
Java program to find the largest among three numbers (With user ...
07:34
Find out Max and Min number out of three positive numbers - Quick ...
04:36
Java Program to Find Largest Among three numbers using if else ...
10:00
Java Program : Find Largest Number out of 3 Numbers | Java ...
TutorialsPoint
tutorialspoint.com โบ java-program-to-find-maximum-of-three-numbers
Java program to find maximum of three numbers
In this program, we initialize three integer variables, num1, num2, and num3, with the values 15, -5, and 7 respectively. We then use an if statement to compare the values. We check if num1 is greater than or equal to both num2 and num3. If this condition is true, num1 is the maximum number, and ...
Programiz
programiz.com โบ java-programming โบ examples โบ largest-number-three
Java Program to Find the Largest Among Three Numbers [if-else & nested if-else]
... public class Largest { public static void main(String[] args) { double n1 = -4.5, n2 = 3.9, n3 = 2.5; if( n1 >= n2 && n1 >= n3) System.out.println(n1 + " is the largest number."); else if (n2 >= n1 && n2 >= n3) System.out.println(n2 + " is the largest number."); else System.out.println(n3 ...
GeeksforGeeks
geeksforgeeks.org โบ java โบ java-program-to-find-the-largest-of-three-numbers
Java Program to Find the Largest of three Numbers - GeeksforGeeks
import java.lang.*; import java.util.*; ... System.out.println(Collections.max(x) + " is the largest number."); } } Output ยท 10 is the largest number....
Published ย November 21, 2020
Javatpoint
javatpoint.com โบ java-program-to-find-largest-of-three-numbers
Java Program to Find Largest of Three Numbers - Javatpoint
Learn basics of Java Program to Find Largest of Three Numbers
Java67
java67.com โบ 2019 โบ 05 โบ how-to-find-largest-and-smallest-of-three-numbers-in-java.html
How to Find the Largest and Smallest of Three Numbers in Java? [Solved] | Java67
import java.util.Scanner; /* * Java Program to find largest and smallest of three numbers */ public class Main { public static void main(String args[]) { // creating scanner to accept radius of circle Scanner scanner = new Scanner(System.in); System.out.println("Welcome in Java program to find largest and smallest of three numbers"); System.out.println("Please enter first number :"); int first = scanner.nextInt(); System.out.println("Please enter second number :"); int second = scanner.nextInt(); System.out.println("Please enter third number :"); int third = scanner.nextInt(); int largest = la
Techcrashcourse
techcrashcourse.com โบ 2017 โบ 02 โบ java-program-find-largest-of-three-numbers.html
Java Program to Find Largest of Three Numbers
Else If A < B, then print the maximum of B and C. package com.tcc.java.programs; import java.util.Scanner; public class MaximumThreeNumbers { public static void main(String[] args) { int a, b, c, max; Scanner scanner; // Take three integer from user scanner = new Scanner(System.in); System.out.println("Enter Three Integer"); a = scanner.nextInt(); b = scanner.nextInt(); c = scanner.nextInt(); // Using if-else statement compare a, b and c if (a > b) { // compare a and c if (a > c) max = a; else max = c; } else { // compare b and c if (b > c) max = b; else max = c; } System.out.println("Largest Number : " + max); } } Output
PrepBytes
prepbytes.com โบ home โบ java โบ greatest of three numbers in java
Greatest of Three Numbers in Java
May 18, 2023 - The ArrayList and Collections.max method approach is the most efficient for finding the largest number from three numbers in Java, especially when dealing with larger datasets. Q4. What are the advantages of the ternary operator approach for finding the largest number from three numbers in Java?
Tutorjoes
tutorjoes.in โบ Java_example_programs โบ three_maximum_number_in_java
Write a program to find maximum between three numbers
import java.util.Scanner; class Three_Maximum { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the Number 1 : "); int num1 = input.nextInt(); System.out.print("Enter the Number 2 : "); int num2 = input.nextInt(); System.out.print("Enter the Number 3 : "); int num3 = input.nextInt(); if(num1>num2 && num1>num3) System.out.println("Maximum Number is " +num1); else if(num1<num2 && num3<num2) System.out.println("Maximum Number is " +num2); else System.out.println("Maximum Number is " +num3); } }
Stack Overflow
stackoverflow.com โบ questions โบ 72552095 โบ finding-the-maximum-of-3-numbers-user-imput-in-3-different-ways-in-java
Finding the maximum of 3 numbers (user imput) in 3 different ways, in java? - Stack Overflow
Both ways boil down to "compare two numbers, take the bigger one, repeat". ... Use one of the available sort methods then pick whichever ends up last. ... Use IntStream.max (or DoubleStream.max). ... int array[] = new int[]{10, 11, 88, 2, 12, 120}; public static int getMax(int[] inputArray){ int maxValue = inputArray[0]; for(int i=1;i < inputArray.length;i++){ if(inputArray[i] > maxValue){ maxValue = inputArray[i]; } } return maxValue; } ... int[] arrayName = new int[] {1, 2, 3}; Arrays.sort(arrayName); System.out.println(arrayName[arrayName.length - 1]);
Naukri
naukri.com โบ code360 โบ library โบ java-program-to-find-the-largest-of-three-numbers
Java Program to Find the Largest of Three Numbers
Almost there... just a few more seconds
BeginnersBook
beginnersbook.com โบ 2017 โบ 09 โบ java-program-to-find-largest-of-three-numbers
Java Program to find largest of three Numbers
public class JavaExample{ public static void main(String[] args) { int num1 = 10, num2 = 20, num3 = 7; if( num1 >= num2 && num1 >= num3) System.out.println(num1+" is the largest Number"); else if (num2 >= num1 && num2 >= num3) System.out.println(num2+" is the largest Number"); else ...