if (number1 >= number2 && number1 >= number3) {
//num1
if (number2 >= number3)
System.out.println(number1 + " " + number2 + " " + number3);
else
System.out.println(number1 + " " + number3 + " " + number2);
}
else if (number2 >= number1 && number2 >= number3) {
//n2
if (number1 >= number3)
System.out.println(number2 + " " + number1 + " " + number3);
else
System.out.println(number2 + " " + number3 + " " + number1);
}
else if (number3 >= number1 && number3 >= number2) {
//num3
if (number1 >= number2)
System.out.println(number3 + " " + number1 + " " + number2);
else
System.out.println(number3 + " " + number2 + " " + number1);
}
The code is working, i just need an improvement please
Resolved
Comments (14)
- Popular
- New
- Old
You must be signed in to leave a comment
Okanlawon Oluwatobi Damilare
23 March 2019, 16:22
try
Sorting three numbers
*/
import java.io.*;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
int a, b, c;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
a = Integer.parseInt(reader.readLine());
b = Integer.parseInt(reader.readLine());
c = Integer.parseInt(reader.readLine());
int [] arr = {a,b,c};
Arrays.sort(arr);
for(int i = arr.length - 1 ; i >= 0; i--){
System.out.printf("%s ", arr[i]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
0
Voland12 Voland
11 March 2019, 19:40
Is such way simpler? // abc acb bac bca cab cba
if(a >= b && a >= c && b >= c){
System.out.print(a + " " + b + " " + c);
}
else if(a >= b && a >= c && c >= b) {
System.out.print(a + " " + c + " " + b);
}
else if(b >= a && b >= c && a >= c) {
System.out.print(b + " " + a + " " + c);
}
else if(b >= a && b >= c && c >= a) {
System.out.print(b + " " + c + " " + a);
}
else if(c >= a && c >= b && a >= b) {
System.out.print(c + " " + a + " " + b);
}
else if(c >= a && c >= b && b >= a) {
System.out.print(c + " " + b + " " + a);
0
Guadalupe Gagnon
25 December 2018, 23:25
put the values in an array, sort the array, read the numbers backwards. I also included top of the code for reference:
+2
Angeget
26 December 2018, 17:48
Thanks for your Answer. But we have not learned Arrays still now.
0
Guadalupe Gagnon
26 December 2018, 18:03
Who is stopping you from learning? Coding isn't about doing everything from the basics up. It is about using/implementing code already written by others. Otherwise you should just learn machine code and start with writing a bios.
+1
Angeget
26 December 2018, 18:07
I appreciate your answer.
As a new baby in java world, Arrays was just very new for me.
0
Angeget
26 December 2018, 18:09
Arrays seem to be very cool. I do not need to implement the code manually as i did. Relly helpfull. THanks
0
Guadalupe Gagnon
26 December 2018, 18:12
I'm sorry, I don't mean to overload you. It took me a little while to start understanding everything myself (and still learning). Now i use google to search for different ways to solve things (and also learn these ways).
0
Angeget
26 December 2018, 18:17
You right Gagnon, it is goog to search also yourself and not being limited on one way. I appreciate the way you do. It encourages me to go beyond what i learned hier and search for other ways also to solve problem. Thank you very much.
0
MP
20 January 2019, 16:03
@ Guadalupe Gagnon, could you explain your code? I'd like to understand it and use it but even if I copy it it won't work.
0
Guadalupe Gagnon
20 January 2019, 17:06
@MP What problem are you having? Does scanner show in red? If that is the case you just are missing an import statement. Whenever code is colored red just click on the code and hit alt+enter for the IDE to suggest fixes. In this case you just need to import the class.
Or are you missing a throw statement? Alt+enter will help you with fixing this as well.
0
MP
20 January 2019, 17:35
Well I mostly use the homepage IDE and not an external program. I used import on the scanner but can't remember the throw instruction. What is it for?
0
Guadalupe Gagnon
20 January 2019, 17:45
The homepage IDE doesn't work but for the first few levels. The harder tasks it can't handle. You have to use an outside IDE to solve these and codegym has an addon specifically for intelliJ to work with codegym directly from that IDE.
0
MP
20 January 2019, 17:49
Okay, then I'm heading back to that lesson and follow the instructions to install it.
0