-2

If i input 1324,3591

It mean i want the bigger denote in order.

int x = 1324;
int y = 3591;
System.out.println(x+y);

I want the output show:

4321 and 8531

kappa
  • 11
  • 1
  • 5

2 Answers2

1

Create two function, public int[] orderAsIncreasing(int input[]) and public int[] orderAsDecreasing(int input[]).

Then, loop inside the input searching for the highest value, and place it as the first/last in the output of each function. Remove that value from the array. Repeat until the array is empty. Then return the result

I will not post code, since this looks like some sort of homework. The logic is there. Now, use google and documentations to get the operators for it.

Bonatti
  • 2,778
  • 5
  • 23
  • 42
  • but i use :Random r = new Random(); int x = r.nextInt(9999)+1; the input should be random number – kappa Nov 26 '15 at 19:19
  • then change to what you need... `public int orderAsIncreasing(int input)`. Done. just `int s = orderAsIncreasing(r);` – Bonatti Nov 26 '15 at 19:27
1

I think you should convert the numbers to strings and then to char arrays. If 'number' is your number, then something like:

String numberAsString = String.valueOf(number);
char[] charArray = numberAsString.toCharArray();
Arrays.sort(charArray);
String newNumberAsString = new String(charArray);
int number2= Integer.valueOf(newNumberAsString);
J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • String numberAsString = String.valueOf(array); char[] charArray = numberAsString.toCharArray(); // in reverse order Arrays.sort(array); System.out.println(Arrays.asList()); System.out.println("The decreasing order:"); for (int i = array.length - 1; i >= 0; i--) System.out.printf(String.format("%04d ", numberAsString); System.out.println(); it can't run – kappa Nov 26 '15 at 20:29
  • I have added some more code to make my point clear. If you need reverse order, you must sort with a different comparator. – J Fabian Meier Nov 27 '15 at 08:11
  • if i want to being a decreasing order for 1325 out 5321 what should i do – kappa Nov 28 '15 at 18:45
  • You have to implement your own comparator. See http://stackoverflow.com/questions/5393254/java-comparator-class-to-sort-arrays – J Fabian Meier Nov 30 '15 at 09:48