11.2 Programming with Recursion
All short statements about programming techniques are false.
—ANONYMOUS
In this section, we present some programs that illustrate ways to use recursion.
Programming Example Insisting That User Input Be Correct
The program in Listing 11.5 simply requests a positive integer and then counts down from that integer to zero. The method getCount
reads the integer from the user. Notice that if the user enters a number that is not positive, the method getCount
calls itself. This call starts the input process all over again from the beginning. If the user enters another incorrect number, another recursive call occurs, and the input process will start yet again. This procedure is repeated until the user enters a positive ...
Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.