Results 1 to 20 of 20
  1. #1
    gardheere50 is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Post Problem with making the numbers display in ascending order?

    Java Code:
    import java.util.Scanner;
    public class Order3
    
    	{
    		 public static void main (String[] args)
    	{
    		Scanner scan = new Scanner (System.in);
    		System.out.println("Enter number 1");
    		int Number1 = scan.nextInt();
    
    		System.out.println("Enter number 2");
    		int Number2 = scan.nextInt();
    
    		System.out.println("Enter number 3");
    		int Number3 = scan.nextInt();
    
    
    
    		if(Number1 > Number2 && Number1 > Number3 && Number2 > Number3) {
    		System.out.println(Number3 + " " + Number2 + " " + Number1);
    		}
    
    		if(Number2 > Number1 && Number2 > Number3 && Number1 > Number3){
    		System.out.println(Number2 + " " + Number1 + " " + Number3);
    		}
    
    		if(Number2 > Number1 && Number2 > Number3 && Number1  < Number3){
    		System.out.println(Number3 + " " + Number2 + " " + Number1);
    		}
    
    		if(Number3 > Number1 && Number3 > Number2 && Number2 > Number1){
    		System.out.println(Number2 + " " + Number3 + " " + Number1);
    		}
    
    		if(Number3 > Number1 && Number3 > Number2 && Number2 < Number1){
    		System.out.println(Number1 + " " + Number3 + " " + Number2);
    		}
    
    	}
    }
    Last edited by gardheere50; 10-29-2012 at 01:08 AM.

  2. #2
    gardheere50 is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Problem with making the numbers display in ascending order?

    Also I want to make this program except numbers that are the same like if the user writes "1 2 1 3" I want the program to display these numbers in order like "1123"..I hope thats makes sense...

  3. #3
    gardheere50 is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Problem with making the numbers display in ascending order?

    Please guys help me..dont just view and move on to next post..

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Problem with making the numbers display in ascending order?

    1. Bumping a thread after just one hour is rude.

    2. Please go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    gardheere50 is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Problem with making the numbers display in ascending order?

    Followed the rules..sorry about that..now whats next and How can I get a solution to my problem..

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

    Default Re: Problem with making the numbers display in ascending order?

    Ask a specific question. I have no idea where you are stuck. "I have no idea how to do this" is not specific and not a question.

  7. #7
    gardheere50 is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Problem with making the numbers display in ascending order?

    basically the program works, however rather than sorting the numbers the user inputted into an ascending order it sorts it in descending order, which where the problem lies. I want to the program to sort the numbers in ascending order...it doesn't do that and Also I want to make this program except numbers that are the same like if the user writes "1 2 1 3" I want the program to display these numbers in order like "1123"..I hope thats makes sense...

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

    Default Re: Problem with making the numbers display in ascending order?

    So you code is working except you have the output in the wrong order.
    Java Code:
    int num1 = 20;
    int num2 = 10;
    if(num1 > num2) {
        System.out.println(num1 + " " + num2);
    }
    In the above code it will print "20 10". If you want it to print "10 20" instead surely you can figure out what simple change needs to be made.

  9. #9
    gardheere50 is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Problem with making the numbers display in ascending order?

    thanks..but my is longer and hard to actually change to make it sort the numbers in ascending order.

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

    Default Re: Problem with making the numbers display in ascending order?

    HTF is it harder to change?
    Java Code:
    System.out.println(Number1 + " " + Number2 + " " + Number3);
    This is what you currently have displaying the numbers in the wrong order. So simply swap Number1 and Number3.

  11. #11
    gardheere50 is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Problem with making the numbers display in ascending order?

    but if I run the program after making the changes and I type in these numbers in this order "1, 3 , 2 " the program outputs " 2, 3 , 1..thats the confusing part for me..I have been trying to make it all day long...

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

    Default Re: Problem with making the numbers display in ascending order?

    Then your conditions in the if statements are incorrect. Since you have not posted your updated code nobody can help you.

  13. #13
    gardheere50 is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Problem with making the numbers display in ascending order?

    sorry I have updated the code..

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

    Default Re: Problem with making the numbers display in ascending order?

    Any chance you will show it to us? Or should we just guess?

  15. #15
    gardheere50 is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Problem with making the numbers display in ascending order?

    in line 20 I have changed the numbers around as you have suggested but still the code does not sort the numbers in ascending order

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

    Default Re: Problem with making the numbers display in ascending order?

    Ffs, show your code!

  17. #17
    gardheere50 is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Problem with making the numbers display in ascending order?

    Java Code:
    		if(Number2 > Number1 && Number2 > Number3 && Number1 > Number3){
    		System.out.println(Number2 + " " + Number1 + " " + Number3);
    		}

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

    Default Re: Problem with making the numbers display in ascending order?

    If your input is 1 3 2 then the if statement above will not be true as Number1 is not greater than Number3. So it must be entering a different if statement. One which you have not shown. You really do not want my help do you? Show your entire code!

  19. #19
    gardheere50 is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Problem with making the numbers display in ascending order?

    Java Code:
    import java.util.Scanner;
    public class Order3
    
    	{
    		 public static void main (String[] args)
    	{
    		Scanner scan = new Scanner (System.in);
    		System.out.println("Enter number 1");
    		int Number1 = scan.nextInt();
    
    		System.out.println("Enter number 2");
    		int Number2 = scan.nextInt();
    
    		System.out.println("Enter number 3");
    		int Number3 = scan.nextInt();
    
    
    
    		if(Number1 > Number2 && Number1 > Number3 && Number2 > Number3) {
    		System.out.println(Number3 + " " + Number2 + " " + Number1);
    		}
    
    		if(Number2 > Number1 && Number2 > Number3 && Number1 > Number3){
    		System.out.println(Number2 + " " + Number1 + " " + Number3);
    		}
    
    		if(Number2 > Number1 && Number2 > Number3 && Number1  < Number3){
    		System.out.println(Number3 + " " + Number2 + " " + Number1);
    		}
    
    		if(Number3 > Number1 && Number3 > Number2 && Number2 > Number1){
    		System.out.println(Number2 + " " + Number3 + " " + Number1);
    		}
    
    		if(Number3 > Number1 && Number3 > Number2 && Number2 < Number1){
    		System.out.println(Number1 + " " + Number3 + " " + Number2);
    		}
    
    	}
    }

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

    Default Re: Problem with making the numbers display in ascending order?

    No pretend you are the computer. Which of those if statements will be true if the input is 1 3 2?
    1 is not greater than 3 so the first one is false.
    Keep going for all of them and test it for yourself. One problem is for 3 numbers there are 6 possible combinations and yet you only have 5 if/print statements.

Similar Threads

  1. search array - ascending order
    By wiola111 in forum New To Java
    Replies: 5
    Last Post: 08-28-2012, 03:09 PM
  2. Sorting in ascending and descending order
    By flpanthers1 in forum New To Java
    Replies: 10
    Last Post: 06-27-2011, 03:48 PM
  3. ascending order using array rush
    By jca in forum New To Java
    Replies: 2
    Last Post: 01-03-2011, 04:24 AM
  4. Checking ascending order of array
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 10:44 PM
  5. How to add coins in ascending order in arraylist
    By tribujohn in forum New To Java
    Replies: 2
    Last Post: 01-23-2009, 04:31 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
  •