/* * This program demonstrates the use of if statements. */ class TwoThreeSorts { public static void main(String[] args) { int argsLength = args.length; if (argsLength < 2 || argsLength > 3) { System.out.println("I need 2 or 3 arguments, please."); System.exit(1); } // If I am here, there are 2 or 3 arguments. int a, b, c = 0; // There are at most 3 ints input by the user. a = Integer.parseInt(args[0]); b = Integer.parseInt(args[1]); // Make sure a <= b by swapping them if necessary. if (a > b) { int t = a; a = b; b = t; } // At this point we know a <= b. if (argsLength == 3) { c = Integer.parseInt(args[2]); // Make sure b <= c by swapping them if necessary. if (b > c) { int t = b; b = c; c = t; } // At this point c is the biggest number but no guarantee // on the values of a & b. // So we make sure a <= b by swapping them if necessary. if (a > b) { int t = a; a = b; b = t; } } System.out.println(a); System.out.println(b); if (argsLength == 3) System.out.println(c); } }