/** * Repetition.java * * @author Jeff Ondich * * This program illustrates while-loops. * * 1. Compile, run, read, try to figure out what's happening. * * 2. See if you can make the greetings count down to 1 rather * than up from 1. ("Hi there #10, Hi there #9...") * */ import javabook.*; class Repetition { public static void main( String[] args ) { // Get ready to talk to the us. MainWindow mw = new MainWindow(); mw.setVisible( true ); mw.toFront(); OutputBox out = new OutputBox( mw ); out.setVisible( true ); out.toFront(); InputBox in = new InputBox( mw ); // Ask for a number. int nGreetings = in.getInteger( "How many greetings do you desire?" ); // Greet the user repeatedly. int j = 1; while( j <= nGreetings ) { out.printLine( "Hi there #" + j ); j = j + 1; } // Provide the user with a handy bit of extra information. int total = 0; j = 1; while( j <= nGreetings ) { total = total + j; j = j + 1; } out.printLine( "The sum of the first " + nGreetings + " integers is " + total ); } }