Zeus web server - 162 Control Structures: Part 1 Chapter 4 18

162 Control Structures: Part 1 Chapter 4 18 // Initialization Phase 19 total = 0; // clear total 20 gradeCounter = 1; // prepare to loop 21 22 // Processing Phase 23 while ( gradeCounter <= 10 ) { // loop 10 times 24 25 // prompt for input and read grade from user 26 grade = JOptionPane.showInputDialog( 27 “Enter integer grade: ” ); 28 29 // convert grade from a String to an integer 30 gradeValue = Integer.parseInt( grade ); 31 32 // add gradeValue to total 33 total = total + gradeValue; 34 35 // add 1 to gradeCounter 36 gradeCounter = gradeCounter + 1; 37 38 } // end while structure 39 40 // Termination Phase 41 average = total / 10; // perform integer division 42 43 // display average of exam grades 44 JOptionPane.showMessageDialog( null, 45 “Class average is ” + average, “Class Average”, 46 JOptionPane.INFORMATION_MESSAGE ); 47 48 System.exit( 0 ); // terminate the program 49 50 } // end method main 51 52 } // end class Average1 Fig. 4.7Class-average program with counter-controlled repetition (part 2 of 3). Fig. 4. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply