1

How can I create a java program using only if / else to order 5 numbers in ascending order. I can not use for processes or array. Numbers are entered by the user My code...

public static void main(String[] args) {
    int a=0;
    int b=0;
    int c=0;
    int d=0;
    int e=0;
    int lugar1=0; 
    int lugar2=0;
    int lugar3=0;
    int lugar4=0;
    int lugar5=0;

    Scanner sc = new Scanner (System.in);
    System.out.println("Enter first number");
    a = sc.nextInt();
    System.out.println("Enter second number");
    b = sc.nextInt();
    System.out.println("Enter third number");
    c = sc.nextInt();
    System.out.println("Enter fourth number");
    d = sc.nextInt();
    System.out.println("Enter fifth number");
    e = sc.nextInt();

    if(a>b&&a>c&&a>d&&a>e)
    {
        lugar1=a;
    }
    else {
        if (b>c&&b>d&&b>e&&b>a)
        {
        lugar1=b;
        }
        else {
            if (c>d&&c>e&&c>a&&c>b)
            {
            lugar1=c;
            }
            else {
                if (d>e&&d>a&&d>b&&d>c)
                {
                lugar1=d;
                }
                else 
                {
                lugar1=e;
                }
            }
        }        
    }

    if(a<b&&a<c&&a<d&&a<e)
    {
        lugar5=a;
    }
    else {
        if (b<c&&b<d&&b<e&&b<a)
        {
        lugar5=b;
        }
        else {
            if (c<d&&c<e&&c<a&&c<b)
            {
            lugar5=c;
            }
            else {
                if (d<e&&d<a&&d<b&&d<c)
                {
                lugar5=d;
                }
                else 
                {
                lugar5=e;
                }
            }
        }        
    }


    System.out.println(lugar1);

    System.out.println(lugar5);
}

I get not do that walk again, I have only 1 and 5

Chris Marasti-Georg
  • 34,091
  • 15
  • 92
  • 137
British User
  • 33
  • 1
  • 7
  • "I get not do that walk again, I have only 1 and 5" what? You are only printing `lugar1` and `lugar5`. `System.out.println(lugar1);` `System.out.println(lugar5);` – DSquare May 30 '14 at 17:52
  • If the number of comparisons limited, then your brute force method should work. What do you mean by "I get not do that walk again"? – Tim Zhukov-Khovanskiy May 30 '14 at 17:52
  • @DSquare He is saying that he does not want to walk again because he has only 1 to 5. Whatever that means xD – An SO User May 30 '14 at 17:55
  • 1
    If you have 3 or less variables it would be not too difficult with some if statements. But the more variables you have the greater difficulty to sort them without using any data structure such as an array or linked list. – berkay May 30 '14 at 17:55
  • Here you only have two "lugar1" & "lugar5" I need the code to calculate the "lugar2", "lugar3", "lugar4" are complicated to find – British User May 30 '14 at 18:04
  • 1
    @user2089181 do this problem with 2 variables. Ok that was easy. Now with 3. More code but it can be done. Now just apply the same logic and go up to 5. Nothing special, just a lot of pain (code). Your teacher is a sadist. He will probably come and say "That sucked right? Well that's why you have to learn loops, sorting algorithms, data structures, ..." – DSquare May 30 '14 at 18:09

3 Answers3

2

Your constraints do not exclude using Lists. See How to sort a ArrayList in Java?

If you can't use Lists either, I suggest trying to use Math.min, http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html, which would allow you to determine which of them is the next minimum...

Create a function to print the next smallest.

private void printNextSmallest() {
  int min = Math.min(Math.min(Math.min(a,b), Math.min(c,d)),e);
  if (min == Integer.MAX) {return;}
  else if (a == min) { System.out.println(a + " "); printNextSmallest(Integer.MAX, b, c, d,e);}
  //..
  else if (e == min) { System.out.println(e + " "); printNextSmallest(a, b, c, d, Integer.MAX);
}
Community
  • 1
  • 1
matt
  • 115
  • 5
  • 1
    His constraints do not exclude implementing this as a web service and connecting to it. I don't think that is the point of the exercise. I mean, the constraints are not exhaustive, but his objective is clear: only use `if/else` – DSquare May 30 '14 at 18:08
  • @DSquare As might be implemented, since I already have the maximum "lugar1" and minimum "lugar5" with only if/else, try various combinations and failed to find anything – British User May 30 '14 at 18:18
1

OK. I bit the bullet and typed it all. No loops, no arrays, no nothing.

The basic idea is to keep sorted order.

import java.util.Scanner;


public class Sort5 {

    static int a1 = Integer.MIN_VALUE, a2 = Integer.MIN_VALUE, a3 = Integer.MIN_VALUE, a4 = Integer.MIN_VALUE, a5 = Integer.MIN_VALUE;


    public static void placeNext(int aNew) {
        if (aNew > a5) {
            a1 = a2;
            a2 = a3;
            a3 = a4;
            a4 = a5;    
            a5 = aNew;
        } else {
            if (aNew > a4) {
                a1 = a2;
                a2 = a3;
                a3 = a4;
                a4 = aNew;
            } else {
                if (aNew > a3) {
                    a1 = a2;
                    a2 = a3;
                    a3 = aNew;
                } else {
                    if (aNew > a2) {
                        a1 = a2;
                        a2 = aNew;
                    } else {
                        a1 = aNew;
                    }
                }
            }
        }
    }



    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        placeNext(sc.nextInt());
        placeNext(sc.nextInt());
        placeNext(sc.nextInt());
        placeNext(sc.nextInt());
        placeNext(sc.nextInt());

        System.out.println("" + a1 + " | " + a2 + " | " + a3 + " | " + a4 + " | " + a5);
    }

}

Unless I made a typo somewhere should work for any 5 ints.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
0

Use these methods:

public int getSmallestInt(int... ints) {
    int smallest = Integer.MAX_VALUE;
    for (int i : ints) {
        smallest = Math.min(smallest, i);
    }
    return smallest;
}

public int getLargestInt(int... ints) {
    int largest = Integer.MAX_VALUE;
    for (int i : ints) {
        largest = Max.max(largest, i);
    }
    return largest;
}

or to sort:

public static void main(String[] args) {
    List<Integer> ints = new Test().sort(5, 1, 2);
    System.out.println(ints.get(0)); //-> 1
}
public List<Integer> sort(Integer... ints) {
    List<Integer> l = Arrays.asList(ints);
    Collections.sort(l);
    return l;
}
tmanion
  • 401
  • 2
  • 11