Source of Reverse600EasyWay.java


  2: import java.util.Scanner;

  4: /**
  5:  * This program reverses 600 numbers entered by the user.
  6:  * It uses an array to make the code much shorter than it would be
  7:  * without an array (for which see Reverse600HardWay.java).
  8:  *
  9:  * @author Mark Young (A00000000)
 10:  */
 11: public class Reverse600EasyWay {

 13:     /** the number of numbers I will reverse */
 14:     public static final int SIX_HUNDRED = 600;

 16:     public static void main(String[] args) {
 17:         // create variables
 18:         Scanner kbd = new Scanner(System.in);
 19:         int[] n = new int[SIX_HUNDRED];

 21:         // introduce yourself
 22:         System.out.print("\n\n"
 23:                 + "A program to reverse " + SIX_HUNDRED
 24:                 + " numbers entered by the user.\n\n");

 26:         // read the numbers
 27:         System.out.println("Enter the " + SIX_HUNDRED + " numbers below:\n");
 28:         for (int i = 0; i < SIX_HUNDRED; i++) {
 29:             n[i] = kbd.nextInt();
 30:         }
 31:         kbd.nextLine();

 33:         // print them out in reverse order
 34:         System.out.println("\n\nIn reverse order they are:\n");
 35:         for (int i = SIX_HUNDRED-1; i > 0; i--) {
 36:             System.out.print(n[i] + ", ");
 37:         }
 38:         System.out.print("and " + n[0] + "\n");

 40:         // print some blank lines to make the ending less abrupt
 41:         System.out.print("\n\n");
 42:     }
 43: }