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.print("\nCounting down from " + i + ": ");
 14:             countDownFrom(i);
 15:         }
 16:         System.out.print("\n\n");
 17:     }

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

 37: }