//File: SuffixLoop2.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 java.awt.*; import CSLib.*; public class SuffixLoop2 { public void doIt() { //KEEP REPEATING THE FOLLOWING //UNTIL THE USER SAYS STOP while (true) { //INPUT SECTION InputBox in = new InputBox(); in.setLocation (150, 150); //Moves the InputBox over //to make the OutputBox easier to see Font myFont = new Font ("Serif", Font.ITALIC+Font.BOLD, 24); in.setFont (myFont); in.setBackground (Color.yellow); in.setForeground (Color.red); 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.setSize (350, 150); 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.setLocation (100, 100); in.setPrompt("Again? (hit OK or type N to quit)"); //Typing N causes eoi() to be false char answer = in.readChar(); //This is what makes the loop end..... if (!in.eoi()) //NOTE: The symbol ! means NOT System.exit(0); }//end while }//end doIt() }