Source of CountDown.java


  1: /**
  2:  * A program that uses a recursive method to count down.
  3:  *
  4:  * @author Mark Young (A00000000)
  5:  */
  6: public class CountDown {

  8:     public static void main(String[] args) {
  9:         System.out.println("\n"
 10:                 + "Counting Down\n"
 11:                 + "-------------");
 12:         for (int i = 0; i <= 20; i += 5) {
 13:             System.out.println("\nCounting down recursively from " + i + ": ");
 14:             countDownFrom(i);
 15:             System.out.println();
 16:         }
 17:         System.out.print("\n\n");
 18:     }

 20:     /**
 21:      * Print the numbers from n down to 1 on a single line.
 22:      * (Uses recursion.)
 23:      *
 24:      * @pre  num >= 0
 25:      * @post the numbers from num down to 1 have been printed on one line.
 26:      *
 27:      * @param num the number to count down from
 28:      */
 29:     public static void countDownFrom(int num) {
 30:         System.out.print("...counting down from " + num + ":\t");
 31:         if (num > 0) {
 32:             // print out the biggest number...
 33:             System.out.println(num);
 34:             // ... then count down from the next smallest number
 35:             countDownFrom(num - 1);
 36:         }
 37:     }

 39: }