Results 1 to 18 of 18
 02082011, 01:15 AM #1Member
 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.
 02082011, 01:17 AM #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.

It can easily be done with if blocks. Why not give it a try?
 02082011, 02:47 AM #4
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!
 02082011, 03:02 AM #5
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 19
 02082011, 09:15 AM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,007
 Blog Entries
 7
 Rep Power
 20
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,
Joscenosillicaphobia: the fear for an empty beer glass
 02092011, 11:54 AM #7Member
 Join Date
 Feb 2011
 Posts
 16
 Rep Power
 0
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.
 02092011, 12:31 PM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 19
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; 02092011 at 12:33 PM.
 02092011, 01:59 PM #9
So swapping.
Unlike pieces of paper, you can't just rearrange 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!
 02092011, 09:59 PM #10Member
 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.
 02092011, 10:03 PM #11
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 }
Last edited by quad64bit; 02092011 at 10:04 PM. Reason: Mistake!
 02092011, 10:03 PM #12
Who cares about d?
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.
 02092011, 10:05 PM #13Who cares about d?
 02092011, 10:07 PM #14
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
 02092011, 10:08 PM #15
 02102011, 12:19 AM #16Member
 Join Date
 Feb 2011
 Posts
 16
 Rep Power
 0
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; 02102011 at 01:10 AM.
 02102011, 07:25 AM #17
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,007
 Blog Entries
 7
 Rep Power
 20
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+cmaxmin;
Joscenosillicaphobia: the fear for an empty beer glass
 02122011, 12:16 AM #18Member
 Join Date
 Feb 2011
 Posts
 16
 Rep Power
 0
Similar Threads

Value should be 7 or 8 digits .If 8 digits, the last should be a character
By renu in forum New To JavaReplies: 1Last Post: 01192011, 09:23 PM 
Arrange the given numbers of the array in descending order
By radhi16 in forum New To JavaReplies: 3Last Post: 01132011, 06:07 PM 
arrange(java.lang.String[][]) in Test cannot be applied to (java.lang.String) arrange
By prizzly in forum New To JavaReplies: 4Last Post: 01012011, 10:52 AM 
Reading random order
By khh717 in forum New To JavaReplies: 6Last Post: 05132010, 10:24 PM 
Math.random in array and reverse order
By eugenechia in forum New To JavaReplies: 4Last Post: 02172010, 03:33 AM
Bookmarks