//File: SuffixLoop.java /* After trying to test all of the possibilities, we decided that it would be nice if the computer could just ask us if we wanted to continue. Then, we wouldn't have to keep running our program again to try different numbers. This brings us to the third of the big three ideas in structured programming, repetition or loops. All we need do is wrap up what we had done before into a loop that keeps repeating for as long as we want it to. */ import CSLib.*; public class SuffixLoop { public void doIt() { //KEEP REPEATING THE FOLLOWING //UNTIL THE USER SAYS STOP while (true) { //INPUT SECTION InputBox in = new InputBox(); in.setPrompt("Please type in a number" + " between 0 and 100."); int number = in.readInt(); String suffix=" "; //CALCULATION SECTION if (number==11||number==12||number==13) suffix = "th"; else //take advantage of the pattern { switch (number % 10) { case 1: suffix = "st"; break; case 2: suffix = "nd"; break; case 3: suffix = "rd"; break; default: suffix = "th"; }//end switch }//end else //OUTPUT SECTION OutputBox out = new OutputBox("Add Suffixes"); out.println ("You gave me the number " + number + ".\n\n\n"); out.println ("That corresponds to the" + " adjective " + number + suffix + "."); //ASK THE USER IF WANTS TO CONTINUE //NOTE: The InputBox method, eoi(), returns true // if we're at the "end of input" // That condition is generated if // the user types nothing in the textfield. in.setPrompt("Again? (hit OK or type N to quit)"); //Typing N causes eoi() to be false char answer = in.readChar(); if (!in.eoi()) //NOTE: The symbol ! means NOT break; }//end while }//end doIt() }