Chapter 11
Around and Around It Goes
IN THIS CHAPTER
Creating program loops
Formulating solutions to problems with loops
Diagnosing loop problems
Chapter 8 has code to reverse the letters in a four-letter word that the user enters. In case you haven’t read Chapter 8 or you just don’t want to flip to it, here’s a quick recap of the code:
c1 = keyboard.findWithinHorizon(".",0).charAt(0);c2 = keyboard.findWithinHorizon(".",0).charAt(0);c3 = keyboard.findWithinHorizon(".",0).charAt(0);c4 = keyboard.findWithinHorizon(".",0).charAt(0); System.out.print(c4);System.out.print(c3);System.out.print(c2);System.out.print(c1);
The code is just dandy for words with exactly four letters, but how do you reverse a five-letter word? As the code stands, you have to add two new statements:
c1 = keyboard.findWithinHorizon(".",0).charAt(0);c2 = keyboard.findWithinHorizon(".",0).charAt(0);c3 = keyboard.findWithinHorizon(".",0).charAt(0);c4 = keyboard.findWithinHorizon(".",0).charAt(0);c5 = keyboard.findWithinHorizon(".",0).charAt(0); System.out.print(c5);System.out.print(c4);System.out.print(c3);System.out.print(c2);System.out.print(c1);
What a drag! You add statements to a program whenever the size of ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access