Must find a maximum of three numbers in java

Possible duplicate:
Find a maximum of 3 numbers in Java with different data types (Basic Java)

Write a program that uses a scanner to read three integers (positive) that displays the largest number of three. (Please fill in without using any of the && or || operators. These operators will be closed in the class soon. Similarly, loops are not required.)

 Some sample run: Please input 3 integers: 5 8 3 The max of three is: 8 Please input 3 integers: 5 3 1 The max of three is 5 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: "); String x = keyboard.nextLine(); String y = keyboard.nextLine(); String z = keyboard.nextLine(); int max = Math.max(x,y,z); System.out.println(x + " " + y + " "+z); System.out.println("The max of three is: " + max); } } 

I want to know what is wrong with this code, and how can I find max when I enter 3 different values.

+6
source share
3 answers

Two things: change the variables x , y , z as int and call the method as Math.max(Math.max(x,y),z) , since it takes only two parameters.

In the 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); 
+22
source

This will help if you provided the error you see. Take a look at http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html and you will see that max returns the maximum between two numbers, so you are probably not compiling the code.

First, resolve all compilation errors.

Then your homework will be to find the maximum of three numbers by comparing the first two together and comparing this maximum result with the third value. You should have enough to find your answer.

+2
source

You should know more about java.lang.Math.max :

  • java.lang.Math.max(arg1,arg2) takes only 2 arguments, but you wrote 3 arguments in your code.
  • The 2 arguments should be double , int , long and float , but your String argument record is in the Math.max function. You need to analyze them in the required type.

You will create a compile - time error due to inconsistencies.

Try executing updated code that will solve your goal:

 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); } } 
+2
source

Source: https://habr.com/ru/post/927226/


All Articles