⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studentgrades.java

📁 学生成绩评定应用程序,声明并操作二维数组!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      classAverageJLabel.setText( "Class average:" );
      contentPane.add( classAverageJLabel );
      
      // set up classAverageJTextField
      classAverageJTextField = new JTextField();
      classAverageJTextField.setBounds( 586, 244, 56, 23 );
      classAverageJTextField.setHorizontalAlignment(
         JTextField.CENTER );
      classAverageJTextField.setEditable( false );
      contentPane.add( classAverageJTextField );
      
      // set properties of application's window
      setTitle( "Student Grades" ); // set title bar string
      setSize( 670, 308 );          // set window size
      setVisible( true );           // display window

   } // end method createUserInterface

   // convert a number to a letter grade
   private String convertToLetterGrade( double grade )
   {
      if ( grade >= 90 )
      {
         return "A";
      }
      else if ( grade >= 80 )
      {
         return "B";
      }
      else if ( grade >= 70 )
      {
         return "C";
      }
      else if ( grade >= 60 )
      {
         return "D";
      }
      else
      {
         return "F";
      }
   
   } // end method convertToLetterGrade

   // calculate and display the student and class average
   private void submitGradesJButtonActionPerformed(
      ActionEvent event )
   {
      // get user input
      String nameOfStudent = studentNameJTextField.getText();
      int test1 = Integer.parseInt( test1JTextField.getText() );
      int test2 = Integer.parseInt( test2JTextField.getText() );
      int test3 = Integer.parseInt( test3JTextField.getText() );

      // add user input to arrays
      studentNames[ studentCount ] = nameOfStudent;
      studentGrades[ studentCount ][ FIRST_TEST ] = test1;
      studentGrades[ studentCount ][ SECOND_TEST ] = test2;
      studentGrades[ studentCount ][ THIRD_TEST ] = test3;

      studentCount++; // increment studentCount

      if ( numericJRadioButton.isSelected() )
      {
         displayNumericGrades();
      }
      else
      {
         displayLetterGrades();
      }
      
      // clear other JTextFields for new data
      studentNameJTextField.setText( "" );
      test1JTextField.setText( "" );
      test2JTextField.setText( "" );
      test3JTextField.setText( "" );
 
      // if ten student grades have been entered
      if ( studentCount == MAXIMUM_STUDENTS )
      {
         // disable submitGradesJButton
         submitGradesJButton.setEnabled( false );
      }

   } // end method submitGradesJButtonActionPerformed

   // display student grades and averages as numbers
   private void displayNumericGrades()
   {
      // add a header to displayJTextArea
      displayJTextArea.setText( 
         "Name\tTest 1\tTest 2\tTest 3\tAverage\n" );

      int studentTotal = 0; // store the student's total grades
      int classTotal = 0;   // store the class's total grades

      for ( int student = 0; student < studentCount; student++ )
      {
         // display student names
         displayJTextArea.append( studentNames[ student ] + "\t" );

         studentTotal = 0; // initialize the student's total grades
         
         for ( int test = 0; test < NUMBER_OF_TESTS; test++ )
         {
            // append each test grade to displayJTextArea
            displayJTextArea.append(
               studentGrades[ student ][ test ] + "\t" );

            // add the test grade to the student's total
            studentTotal += studentGrades[ student ][ test ];

         } // end inner for

         // add the student's total grade to the class's total
         classTotal += studentTotal;

         // calculate the student average and display it
         double studentAverage = 
            ( double ) studentTotal / NUMBER_OF_TESTS;
         displayJTextArea.append( 
            twoDigits.format( studentAverage ) + "\n" );

      } // end outer for

      // calculate the class average and display it
      double classAverage = 
         ( double ) classTotal / studentCount / NUMBER_OF_TESTS;
      classAverageJTextField.setText( 
         twoDigits.format( classAverage ) );

   } // end method displayNumericGrades

   // display student grades and averages as letters
   private void displayLetterGrades()
   {
      // add a header to displayJTextArea
      displayJTextArea.setText( 
         "Name\tTest 1\tTest 2\tTest 3\tAverage\n" );

      int studentTotal = 0; // store the student's total grades
      int classTotal = 0;   // store the class's total grades

      for ( int student = 0; student < studentCount; student++ )
      {
         // display student names
         displayJTextArea.append( studentNames[ student ] + "\t" );

         studentTotal = 0; // initialize the student's total grades

         for ( int test = 0; test < NUMBER_OF_TESTS; test++ )
         {
            // append each test grade to displayJTextArea
            displayJTextArea.append( convertToLetterGrade( 
               studentGrades[ student ][ test ] ) + "\t" );

            // add the test grade to the student's total
            studentTotal += studentGrades[ student ][ test ];

         } // end inner for

         // add the student's total grade to the class's total
         classTotal += studentTotal;

         // calculate the student average and display it
         double studentAverage = 
            ( double ) studentTotal / NUMBER_OF_TESTS;
         displayJTextArea.append( 
            convertToLetterGrade( studentAverage ) + "\n" );

      } // end outer for

      // calculate the class average and display it
      double classAverage = 
         ( double ) classTotal / studentCount / NUMBER_OF_TESTS;
      classAverageJTextField.setText( 
         convertToLetterGrade( classAverage ) );

   } // end method displayLetterGrades

   // user selected numeric display
   private void numericJRadioButtonActionPerformed(
      ActionEvent event )
   {
      displayNumericGrades();

   } // end method numericJRadioButtonActionPerformed
   
   // user selected letter display
   private void letterJRadioButtonActionPerformed(
      ActionEvent event )
   {
      displayLetterGrades();
       
   } // end method letterJRadioButtonActionPerformed

   // main method
   public static void main( String[] args ) 
   {
      StudentGrades application = new StudentGrades();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      
   } // end method main

} // end class StudentGrades

/**************************************************************************
 * (C) Copyright 1992-2004 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -