Problem
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<%dn",smallest, mid, greatest);
TextIO.putf("%s<%s<%sn","a", "b", "c");
}
else if(a<c && a<b && c<b){ // a<c<b
TextIO.put("n");
TextIO.putln("ordered:");
TextIO.putf("%d<%d<%dn",smallest, mid, greatest);
TextIO.putf("%s<%s<%sn","a", "c", "b");
}
else if(b<a && b<c && a<c){ // b<a<c
TextIO.put("n");
TextIO.putln("ordered:");
TextIO.putf("%d<%d<%dn",smallest, mid, greatest);
TextIO.putf("%s<%s<%sn","b", "a", "c");
}
else if(b<c && b<a && c<a){ // b<c<a
TextIO.put("n");
TextIO.putln("ordered:");
TextIO.putf("%d<%d<%dn",smallest, mid, greatest);
TextIO.putf("%s<%s<%sn","b", "c", "a");
}
else if(c<a && c<b && a<b){ // c<a<b
TextIO.put("n");
TextIO.putln("ordered:");
TextIO.putf("%d<%d<%dn",smallest, mid, greatest);
TextIO.putf("%s<%s<%sn","c", "a", "b");
}
else if (c<b && c<a && b<a){ //c<b<a
TextIO.put("n");
TextIO.putln("ordered:");
TextIO.putf("%d<%d<%dn",smallest, mid, greatest);
TextIO.putf("%s<%s<%sn","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=%dn",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<%dn",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=%dn",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<%dn",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=%dn",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<%dn",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=%dn",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=%dn",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=%dn",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=%dn",smallest, mid, greatest);
TextIO.putf("%s<%s=%s","c", "b", "a");
}
}
}
Solution
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.