Results 1 to 9 of 9
  1. #1
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default input 3 numbers output order least to greatest

    Can someone please explain why when i put in 2,1,3 it doesn't exicute anything? To me it looks right, the debug doesn't stop, it just keeps going. it Should output 1,2,3.
    Java Code:
    if ((num1<num2) && (num2<num3) && num3>num1)
    			{
           System.out.println(num1+" "+num2+" "+num3);
    			}
    if ((num3<num1) && (num1<num2)&& num2 > num3)
    			{
           System.out.println(num3+" "+num1+" "+num2);
    		     }
    if ((num2<num3) && (num3<num1) && num1>num2)
    			{
    	System.out.println(num2+" "+num3+" "+num1);
    			}
    Last edited by rizowski; 01-14-2011 at 02:53 AM.

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

    Default

    Why don't you simply replace your variables with the corresponding values?

    For input 2 1 3
    num1 = 2
    num2 = 1
    num3 =3

    Now look at your code
    First if:
    if ((num1<num2)
    if 2 < 1
    false

    Second if:
    if ((num3<num1)
    if 3 < 2
    false

    Third if:
    if num3<num1
    Aleady established above it is false.

    Therefore all if statements are false so your code does nothing.

    This is better achieved with nested if/else statements. Or even better place values in an array or List and sort them.

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

    Default

    Another thing with 3 numbers there are 6 possible combinations, whereas your code only prionts out 3 combinations. So clearly your code is lacking 3 more cases.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Look carefully at the conditionals for that input (none will ever be met). What exactly is the goal? Sorting?

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    like Junky said, create an array, prompt the user for a size, then initialize the array to that size, then prompt for items and store each in the array, finally, you can use a loop to print the array.

    However, if you are unfamiliar with array usage, or you want to do it/have to do it with if/else clauses

    you need to have every possible combination thought of, first, what if some numbers are equal? You are simply checking if one is less, if the input is 3, 2, 3, what should it do?

    Second, you need, like stated previously, 6 combinations for an input length of 3, 3 * 2 * 1 = possible combinations.
    now, the combos should be
    (1 = item1, 2 = item2, 3 = item3)
    1, 2, 3
    1, 3, 2
    ...

    make one clause for each of them, include checking <= instead of simply < and you should be able to sort them correctly.
    Last edited by sunde887; 01-14-2011 at 03:38 AM.

  6. #6
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Thanks :D

    Thanks everyone for your posts. I greatly appreciate all of it :D.
    I like it when people post their resolution on a board. so I ended up going with an array and wrote this: (I was aiming for a sort function)
    Java Code:
    public static void main(String[] args) {
    		int [] arra;
    		arra = new int[3];
    		
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Please enter three numbers");
    	        int num1 = scan.nextInt();
    		scan.nextLine();
    		int num2 = scan.nextInt();
    		scan.nextLine();
    		int num3 = scan.nextInt();
    		
    		arra[0]=num1;
    		arra[1]=num2;
    		arra[2]=num3;
    		
    		Arrays.sort(arra);
    		
    		for (int incr = 0; incr < 3; incr++)
    			System.out.println(arra[incr]);
    	}
    Thanks again for everyones response :D
    Last edited by rizowski; 01-14-2011 at 07:41 AM.

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by rizowski View Post
    Java Code:
    for (int incr = 0; incr < 3; incr++)
    Don't have habit of using hard coded values for array iterations, use the length property instead. Like,
    Java Code:
    for (int incr = 0; incr < arra.length; incr++)

    There is nothing wrong with your code, but just a small suggestion. A wrong value sometimes can lead to ArrayIndexOutOfBoundsException.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    with the change goldest suggested you can also allow for a larger amount of numbers to be used. if you print with
    Java Code:
    incr < arra.length
    You can also start the program with
    Java Code:
    System.out.println("Please enter how many numbers:");
    int max = scan.nextInt();
    arra = new int[max];
    Since you are using the
    Java Code:
    array.sort
    instead of a series of if else clauses it is very simple.

    finally, you can have a loop that stores the numbers in an array for you instead of creating three new int variables and then manually entering them all

    just have each step in the loop prompt the user and store the item in the i position of the array.

    These are really just some things you can think about in the future to make your code more, reusable, I suppose, not really sure what word to use

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default

    If you want to avoid all those ugly if-statements for finding the order of those three numbers, have a look at this:

    Java Code:
    int min= Math.min(num1, Math.min(num2, num3));
    int max= Math.max(num1, Math.max(num2, num3));
    int mid= num1+num2+num3-min-max;
    Don't turn this in as if it were your homework; your teacher might call for psychiatric help. This was just for fun ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 3
    Last Post: 01-13-2011, 06:07 PM
  2. formatting numbers in output
    By andy3 in forum New To Java
    Replies: 3
    Last Post: 05-27-2010, 06:43 PM
  3. Replies: 0
    Last Post: 10-14-2009, 05:52 PM
  4. printing number of pos/neg numbers input?
    By shroomiin in forum New To Java
    Replies: 12
    Last Post: 09-25-2009, 02:15 AM
  5. Truncating decimal numbers in the output
    By gbade in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 06:25 PM

Posting Permissions

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