0
Create a list with the numbers you want: | int x, y, z; | // ... load their values ... | List<Integer> numbers = new List<>(); | numbers.add(x); | numbers.add(y); | numbers.add(z); Then sort it this way: | Collections.sort(numbers); | Collections.reverse(numbers); Or that way: | Collections.sort(numbers, Collections.reverseOrder()); And print: | System.out.println(numbers.get(0) + ", " + numbers.get(1) + ", " + numbers.get(2)); Or create an Integer[] array (not int[] array!) and sort that: | Integer[] array = new Integer[] { x, y, z }; | Arrays.sort(array, Collections.reverseOrder()); | System.out.println(array[0] + ", " + array[1] + ", " + array[2]); Or if you always have only three ints you of course can use if's and bubbleshort: | int h; | if (y > x) { // switch x y | h = x; | x = y; | y = h; | } | if (z > x) { // switch x z | h = x; | x = z; | z = h; | } | if (z > y) { // switch y z | h = y; | y = z; | z = h; | } | System.out.println(x + ", " + y + ", " + z); You can use Java8 streams too: | int[] result = Stream.of(x, y, z) | .sorted(Collections.reverseOrder()) | .mapToInt(i -> i) | .toArray(); Or similarly as you did code all the possible outcomes, which is here 3*2*1=6 which is bearable, - and altrough it would be the fastest implementation, its code would be long and not really readable. Do not forget to import what you use from my codes, insert these lines to the top of the code: | import java.util.Collections; | import java.util.stream.Stream;
12th Sep 2018, 2:59 PM
Magyar DĂĄvid
Magyar DĂĄvid - avatar