average1.java

来自「一个JAVA程序,可以对用户输入的几个数字 求其平均数」· Java 代码 · 共 44 行

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

public class Average1 {

   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 gradeCounter

      // 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 counter
         gradeCounter = gradeCounter + 1; // increment counter

      } // end while  

   // terminate 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

⌨️ 快捷键说明

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