Results 1 to 18 of 18
  1. #1
    Dev23 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default 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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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!

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by quad64bit View Post
    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.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Dev23 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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 12:33 PM.

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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!

  10. #10
    Dev23 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    ^ 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.

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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 10:04 PM. Reason: Mistake!

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.

  13. #13
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

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

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by quad64bit View Post
    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.

  16. #16
    Dev23 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by quad64bit View Post
    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 01:10 AM.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    Dev23 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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.

Similar Threads

  1. Replies: 1
    Last Post: 01-19-2011, 09:23 PM
  2. Replies: 3
    Last Post: 01-13-2011, 06:07 PM
  3. Replies: 4
    Last Post: 01-01-2011, 10:52 AM
  4. Reading random order
    By khh717 in forum New To Java
    Replies: 6
    Last Post: 05-13-2010, 10:24 PM
  5. Math.random in array and reverse order
    By eugenechia in forum New To Java
    Replies: 4
    Last Post: 02-17-2010, 03:33 AM

Posting Permissions

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