0
\$\begingroup\$
import bwi.prog.utils.TextIO;

    public class MinMidMax {

        public static void main(String[] args) {

            int a,b,c;

            int greatest, mid, smallest;

            TextIO.putln("Enter three numbers");
            TextIO.put("a=");
            a = TextIO.getInt();
            TextIO.put("b=");
            b = TextIO.getInt();
            TextIO.put("c=");
            c = TextIO.getlnInt();

            greatest = Math.max(a, Math.max(b,c));
            smallest = Math.min(a, Math.min(b,c));

            if (a < greatest && a > smallest )
                mid = a;
            else if (b < greatest && b > smallest )
                mid = b;
            else
                mid = c;

            if(a<b && a<c && b<c){ // a<b<c
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d<%d<%d\n",smallest, mid, greatest);
                TextIO.putf("%s<%s<%s\n","a", "b", "c");
            }
            else if(a<c && a<b && c<b){ // a<c<b
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d<%d<%d\n",smallest, mid, greatest);
                TextIO.putf("%s<%s<%s\n","a", "c", "b");
            }
            else if(b<a && b<c && a<c){ // b<a<c
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d<%d<%d\n",smallest, mid, greatest);
                TextIO.putf("%s<%s<%s\n","b", "a", "c");
            }
            else if(b<c && b<a && c<a){ // b<c<a
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d<%d<%d\n",smallest, mid, greatest);
                TextIO.putf("%s<%s<%s\n","b", "c", "a");
            }
            else if(c<a && c<b && a<b){ // c<a<b
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d<%d<%d\n",smallest, mid, greatest);
                TextIO.putf("%s<%s<%s\n","c", "a", "b");
            }
            else if (c<b && c<a && b<a){                                  //c<b<a
            TextIO.put("\n");
            TextIO.putln("ordered:");
            TextIO.putf("%d<%d<%d\n",smallest, mid, greatest);
            TextIO.putf("%s<%s<%s\n","c", "b", "a");
            }
            else if ( a==b && b==a && a>c && b > c){  // c<a=b
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d<%d=%d\n",c, a, b);
                TextIO.putf("%s<%s=%s","c", "a", "b");

            }
            else if ( a==b && b==a && a<c && b < c){   //a=b<c
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d=%d<%d\n",a, b, c);
                TextIO.putf("%s=%s<%s","a", "b", "c");    
            }
            else if ( a==c && c==a && a>b && c > b){ //b<a=c
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d<%d=%d\n",b, a, c);
                TextIO.putf("%s<%s=%s","b", "a", "c");    
            }
            else if ( a==c && c==a && a<b && c<b){ //a=c<b
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d=%d<%d\n",a, c, b);
                TextIO.putf("%s=%s<%s","a", "c", "b");
            }
            else if ( a<b && a<c && b==c && c==b){   //a<b=c
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d<%d=%d\n",a, b, c);
                TextIO.putf("%s<%s=%s","a", "b", "c");
            }
            else if ( b==c && c==b && c<a && b< a)  // b=c<a
            {
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d=%d<%d\n",b, c, a);
                TextIO.putf("%s=%s<%s","b", "c", "a");

            }

            else if (a == b && a == c && b == c && b == a && c==b && c==a)   //a=b=c
            {
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d=%d=%d\n",smallest, mid, greatest);
                TextIO.putf("%s=%s=%s","a", "b", "c");
            }
            else if (a < b && a < c && b == c && c==b)   //a<b=c
            {
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d<%d=%d\n",smallest, mid, greatest);
                TextIO.putf("%s<%s=%s","a", "b", "c");
            }
            else if (b<a && b<c && a == b && b == a)   //b<a=c
            {
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d<%d=%d\n",smallest, mid, greatest);
                TextIO.putf("%s<%s=%s","b", "a", "c");
            }
            else if (c<a && c<b && a == b && b == a)   //c<a=b
            {
                TextIO.put("\n");
                TextIO.putln("ordered:");
                TextIO.putf("%d<%d=%d\n",smallest, mid, greatest);
                TextIO.putf("%s<%s=%s","c", "b", "a");
            }

    }

}
\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

This will achieve the same goal in much less code.

import bwi.prog.utils.TextIO;
import java.util.*;

public class MinMidMax {

    public static void main(String[] args) {            
        Map<Integer, Character> vars = new TreeMap<Integer, Character>();
        // get input
        TextIO.putln("Enter three numbers");  
        for (Character c = 'a'; c <= 'c'; c++) {
            TextIO.putf("%s=", c);
            vars.put(TextIO.getInt(), c);
        }
        // print the result
        StringBuilder sbValues = new StringBuilder("\nordered:\n");
        StringBuilder sbVars = new StringBuilder();
        for (Map.Entry<Integer, Character> e : vars.entrySet()) {
            sbValues.append(e.getValue());
            sbValues.append("\t<");
            sbVars.append(e.getKey());
            sbVars.append("\t<");
        }
        sbVars.setLength(sb.length() - 2);
        sbValues.setLength(sb.length() - 2);
        TextIO.putln(sbValues);
        TextIO.putln(sbVars);
    }
}

The TreeMap class automatically sorts its keys, and iterating through them is relatively trivial.

Not that StringBuilder has been used to build strings for later output, so you can remove the remaining \t< at the end.

The java.util package can make your life much easier.

Note that this also supports an arbitrary amount of inputs, just change c <= 'c' to any character greater than 'c' on the ASCII table.

\$\endgroup\$
4
  • \$\begingroup\$ I would use a TreeMap<Integer,Character> instead. TreeMap automatically keeps its keys sorted, and doesn't require an explicit Collections.sort(). Also, using Character avoids the ""+c hack. \$\endgroup\$ Sep 25, 2013 at 5:14
  • 1
    \$\begingroup\$ You also don't need items — you can just get it from vars.keySet(), which is guaranteed to be a SortedSet if vars is a TreeMap. \$\endgroup\$ Sep 25, 2013 at 5:18
  • \$\begingroup\$ The Map doesn't handle duplicate values well at all, though! \$\endgroup\$ Sep 25, 2013 at 5:19
  • \$\begingroup\$ @200_success Modified to address first two comments. Changed to TreeMap<Integer, Character> and used resulting entrySet() with StringBuilder. \$\endgroup\$
    – azz
    Sep 25, 2013 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.