0

How to take the 3 input of user, (number [ double ] ) and display the greatest number in java?

Like this: Enter first number: 20 Enter second number: 30 Enter third number: 40 Expected output The greatest number is: 40

21st Nov 2019, 12:28 PM
Reya Patriarca
Reya Patriarca - avatar
4 Respostas
+ 1
Reya Patriarca declare 4 variables if you want and then use conditional statements to get the result. Edit: It can also be done in other ways.
21st Nov 2019, 12:59 PM
Avinesh
Avinesh - avatar
0
Scanner scan = new Scanner (System.in); double first = scan.nextDouble(); double second = scan.nextDouble(); double third = scan.nextDouble(); You can also use an array or you can use a loop: for (int i = 0; i < 3; i++){ double input = scan.nextDouble(); } It depends on your skills if you need to store all values to get the max or if you are able to get it by using one loop.
21st Nov 2019, 12:54 PM
Denise Roßberg
Denise Roßberg - avatar
0
Scanner input = new Scanner(System.in); System.out.println("Type in three numbers (doubles)"); System.out.println("on a single line:-"); String[] mystring = input.nextLine().split(" "); if (mystring.length == 3) { Double first_double = Double.parseDouble(mystring[0]); Double second_double = Double.parseDouble(mystring[1]); Double third_double = Double.parseDouble(mystring[2]); System.out.println(Double.max(Double.max(first_double, second_double), third_double)); } input.close(); }
21st Nov 2019, 7:47 PM
rodwynnejones
rodwynnejones - avatar
0
Thanks for all suggestions 🙏😊😀
22nd Nov 2019, 9:30 AM
Reya Patriarca
Reya Patriarca - avatar