# Thread: How do i arrange 3 random digits in desending order

1. Member
Join Date
Feb 2011
Posts
16
Rep Power
0

## How do i arrange 3 random digits in desending order

Hi, i'm new to this site and Java, obviously.

I've been frustrated for a while now on this. For a class assignment, i have to create a user input where someone can input 3 digits in any order, to then have the function output them from greatest to least. I feel like a understand user input, i just don't know how to have java output numbers from decending order. I've seen a couple of options online by using the 'swapping method' but only with two integers. IF someone could help i would appreciate it. Thanks.

2. This is easily achieved by placing your numbers into an array and then sorting it. Can you use arrays? If not then you will need three variables to stroe the values and then use a large nested if statement to get the order.

3. Yeah, this would be a much harder assignment if you didn't know how many numbers were going to be input into the system. But you have only 3. If you did this on paper, and I gave you 3 strips of paper with numbers on them at random, how would you sort them and give them back to me?
Let me see, is slip 1 less than slip 2? Yes? ok, swap them
Is the new slip 2 less than slip 3? No? Leave it alone.
Hand back the slips in order. Try it on paper.

Then have a loot at if() else() statements, and conditional and comparison operators in java. Google and your textbook are your friends!

Good Luck!

Let me see, is slip 1 less than slip 2? Yes? ok, swap them
Is the new slip 2 less than slip 3? No? Leave it alone.
Hand back the slips in order. Try it on paper!
See, that's all. Forget about the coding first of all, and think how you going to solve in day-to-day life. Write the logic in a paper as quad64bit explain here, and later convert the logic into a Java code.

5. Also have a look at the Math.max( ... ) and Math.min( ... ) methods. They'll clean up your code and you get rid of all those if ... else if ... statements.

kind regards,

Jos

6. Member
Join Date
Feb 2011
Posts
16
Rep Power
0
Originally Posted by Junky
This is easily achieved by placing your numbers into an array and then sorting it. Can you use arrays? If not then you will need three variables to stroe the values and then use a large nested if statement to get the order.
I haven't learned arrays yet. Just conditionals/boolean operators/logic operators, and nesting. I'm trying to create a code that runs three different numbers in three scenarios like this:

2 1 3

If a is less than c and a is greater than 1, then swap 3 (c) with 2 (a) to get 312.

1 2 3

If a is less than b and a is less than c, then switch a and c again to get 321.

3 2 1

Finally, if a is greater than b and a is greater than c, then don't switch anything. Answer: 3 2 1.

I'm just a bit confused on the swapping technique.

7. What happen if I put 3 1 2 ?

That's think about all the combination. Since you've only three numbers it's easy, but if you've four numbers.... That's why you need the workaround with a proper logic.

Say you take first two numbers at a time, and you can easily arrange them. Once you take the third digit, what you should do, and with next and so on. Any idea?
Last edited by Eranga; 02-09-2011 at 01:33 PM.

8. So swapping.

Unlike pieces of paper, you can't just re-arrange variables. So you have to get a little tricky with it. Lets pretend the 3 slips were glued to the table, so you couldn't actually move them around. But lets also say that they were written in pencil, and you were given an extra slip of paper to work with.

If we have 3 slips: A=1, B=3, C=2
and one blank slip: D=
Lets see how we could swap.
If we wanted to swap A and B, we could simply reassign A=B and B = A. But then we loose the original value of A.
Instead, lets write the value of A onto our extra slip D.
Then copy B to A. How do we get the old A value into B now? Well its on D! So B = D.

The swap can be done in 3 very small lines of code. Try it on real life like I described, then try it with code once you figure it out.

Good luck!

9. Member
Join Date
Feb 2011
Posts
16
Rep Power
0
^ Okay, so i feel like i've figured it out, but now that i've switched each number how do i get my new "d" value back to an empty value?

Heres my code:

public class swappingthree {
public static void main (String args [])
{
int a,b,c,d;
a = 1;
b = 3;
c = 2;
if (a < b && a < c)
d = a; //switches 2 with 1. New number "1,3,2,1"
b = a; //switches 3 with 1. New number "3, 3, 2, 1"
c = b; //switches 2 with 3. New number "3, 2, 2, 1"
d = c; //switches 1 with 2. New number "3,2,1,1"
//How do i make it so that d is an empty value, to where
//my new number isn't a four digit number?

}
}

**************

Oh and i can't seem to get this to run.

10. First off, you forgot {} on your if statement. Secondly, I wouldn't try to do too much in each if statement. You can have as many ifs as you like.

Try something like
Java Code:
```if(a < b){
//swap a and b using d
}
if(b < c){
//same thing again
}```
Does that help?
Last edited by quad64bit; 02-09-2011 at 11:04 PM. Reason: Mistake!

All you need to do is sort a, b & c. If you manage that and you get the three value in the correct order then all you need to do is print a b & c. Forget d.

We don't need it in the end, but D is needed for swapping, careful not to confuse the OP.

13. But then again you do not need to sort them at all. Just use nested if statements like I said at the beginning.
Java Code:
```if a is less than b and a is less than c
then a is the smallest (but what about b and c? use nested if statement)
if b is less than c
then c is the largest so order is a b c (going from small to large)
//rest of if statements```

We don't need it in the end, but D is needed for swapping, careful not to confuse the OP.
Yeah, that was the point I was trying to make ;)

You can overwrite the value in d as many times as you like but whatever value it ends up with is of no consequence. Just don't print it.

15. Member
Join Date
Feb 2011
Posts
16
Rep Power
0
First off, you forgot {} on your if statement. Secondly, I wouldn't try to do too much in each if statement. You can have as many ifs as you like.

Try something like
Java Code:
```if(a < b){
//swap a and b using d
}
if(b < c){
//same thing again
}```
Does that help?
Yeah, what i didn't realise was that i needed to put in 'System.out.println( ...);' in. I didn't know that it accepts numbers as well as strings.

This is my code so far i'm still trying to figure out how to get all of the six possible combinations.
Java Code:
```public class swappingthree {
public static void main (String args [])
{
int a,b,c;
//1, 3, 2
a = 1; 	b = 3;	c = 2;
if (a < c && a < b)
{
System.out.println("New number 1");
System.out.println(a = b);
System.out.println(b = c);
System.out.println(c = b -1);}

// 3, 2, 1
a = 3; b = 2; c = 1;
if (a == 3 && b == 2 && c == 1) {
System.out.println("New number 2");
System.out.println(a = a);
System.out.println(b = b);
System.out.println(c = c);
}

//3, 1, 2
a = 3; b = 1; c = 2;
if (a > b && a > c) {
System.out.println("New Number 3");
System.out.println(a = a);
System.out.println(b = c);
System.out.println(c = b - 1);
}

}
}```
Last edited by Dev23; 02-10-2011 at 02:10 AM.

16. Why did you ignore my hint in reply #6? Have a look:

Java Code:
```int a= ...; b= ...; c= ...;
int max= Math.max(a, Math.max(b, c));
int min= Math.min(a, Math.min(b, c));
int mid= a+b+c-max-min;```
kind regards,

Jos

17. Member
Join Date
Feb 2011
Posts
16
Rep Power
0
Originally Posted by JosAH
Why did you ignore my hint in reply #6? Have a look:

Java Code:
```int a= ...; b= ...; c= ...;
int max= Math.max(a, Math.max(b, c));
int min= Math.min(a, Math.min(b, c));
int mid= a+b+c-max-min;```
kind regards,

Jos
I thought i had it but apparently it wasn't
the method he was looking for. I'll try this way if he doesn't give me the solution.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•