e4_5.java

来自「java 初学者学习实例」· Java 代码 · 共 62 行

JAVA
62
字号
// Fig. 4.7: Average1.java
// Class-average program with counter-controlled repetition.
import javax.swing.JOptionPane;

public class E4_5 {

   public static void main( String args[] ) 
   {
      int total;          // sum of grades input by user
      int gradeCounter;   // number of grade to be entered next
      int grade;          // grade value
      int average;        // average of grades

      String gradeString; // grade typed by user
   
      // initialization phase
      total = 0;          // initialize total
      gradeCounter = 1;   // initialize loop counter
   
      // processing phase
      while ( gradeCounter <= 10 ) {  // loop 10 times

         // prompt for input and read grade from user
         gradeString = JOptionPane.showInputDialog( 
            "Enter integer grade: " );

         // convert gradeString to int
         grade = Integer.parseInt( gradeString );

         total = total + grade;            // add grade to total
         gradeCounter = gradeCounter + 1;  // increment counter

      } // end while
   
      // termination phase
      average = total / 10;  // integer division

      // display average of exam grades
      JOptionPane.showMessageDialog( null, "Class average is " + average, 
         "Class Average", JOptionPane.INFORMATION_MESSAGE );

      System.exit( 0 );  // terminate the program

   } // end main

} // end class Average1

/**************************************************************************
 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and               *
 * Prentice Hall. All Rights Reserved.                                    *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?