java achieves the average score of each subject-【simple application】-T5

A basic java application problem

1. There is a file named Java_2.java in the candidate’s folder. The program is incomplete, please comment on the line "//********* Found ******** **" Fill in the correct content in the underline of the statement in the next line, then delete the underline. Do not delete the comment line or modify the content of other existing statements. The file must be stored in the candidate's folder when saving, and the file name of the original file must not be changed. .

The function of the following program is to give some students' scores in several courses, calculate the highest score of all the scores, and the average score of each class of each student.

code show as below:

import java.awt.*;
import javax.swing.*;

public class Java_2{
    
    
    int grades[][] = {
    
     {
    
     77, 68, 86, 73 },
                        {
    
     96, 87, 89, 81 },
                        {
    
     70, 90, 86, 81 } };
    int students, exams;
    String  output;
    JTextArea outputArea;
    
    public Java_2(){
    
        
        students = grades.length;
        exams = grades[ 0 ].length;
        
        JFrame f = new JFrame();
        f.setSize(300,300);
        f.setVisible(true);//显示
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭
        //*********Found**********
        outputArea = new ________________();
        Container c = f.getContentPane();
        //*********Found**********
        c.add( ________________ );
        
        output = "数组是:";
        buildString();
        output += "\n\n最高分: " + maximum() + "\n";
         //*********Found**********
        for ( int i = 0; i < ________________; i++ )
            output += "\n第" + (i+1) + "个学生的平均分是: " +
                    average( grades[ i ] );
         //*********Found**********
        outputArea.________________( output );
    }
    
    
    //找最高分
    public int maximum(){
    
    
        int highGrade = 0;
        for ( int i = 0; i < students; i++ )
            for ( int j = 0; j < exams; j++ )
                if ( grades[ i ][ j ] > highGrade )
                     //*********Found**********
                    highGrade = ________________;
        return highGrade;
    }
    //对各组学生确定平均分
    public int average( int setOfGrades[] ){
    
    
        int total = 0;
        for ( int i = 0; i < setOfGrades.length; i++ )
             //*********Found**********
            total += ________________;
       
        return total /exams;
    }
    //输出格式
    public void buildString(){
    
    
        output += "        ";
        for ( int i = 0; i < exams; i++ )
            output += "[" + i + "]   ";
        for ( int i = 0; i < students; i++ ) {
    
    
            output += "\ngrades[" + i + "]   ";
            for ( int j = 0; j < exams; j++ )
                output += grades[ i ][ j ] + "   ";
        }
    }
    
    public static void main(String[ ]args){
    
    
        new Java_2();
    }
}

The results of the program are as follows:
Insert picture description here

This question focuses on the examinee 's mastery of the Java language array and text box drawing .

The first space in this question : It can be seen from the program running results that it is finally displayed as a text area, so the space here is filled in JTextArea ;
Insert picture description here

The second space in this question : after the container is initialized, you need to add the specific display content, here is the created JTextArea object, so the space here is filled in outputArea ;
click to learn about JTextArea

The third box in this question : To calculate the average score of each student, it is necessary to cycle with the number of students as the maximum value, so fill in the box here with students ;

The fourth space in this question : The newly created JTextArea object needs to set the displayed content, using the setText method, so the space here is filled with setText ;

The fifth space in this question : All grades are placed in the two-dimensional array grades, so the maximum value of each two-dimensional array must be compared and stored in highGrade. When the two-dimensional array value is greater than highGrade, the The value is stored, so the space here is filled with grades[ i] [j] ;

The sixth box in this question : Before calculating the average score, you need to calculate the total score. You must add all the grades of each student, so fill in the box here with setOfGrades[i] .
This review is for reference only.

Learning some methods in the code:

setVisible(boolean) method: The
setVisible(boolean) method is used to show/hide GUI components.
Use true if you need to display, false if you need to hide.
The setVisible(true) method means that the data model has been constructed, allowing the JVM to execute the paint method according to the data model to start drawing and display it on the screen. Instead of displaying graphics, it can run and start drawing. SetVisible( The) method is placed at the end, and the code is executed in order. If setVisible() is placed in the front and other components are added later, it may not be displayed.

The complete code is as follows:

import java.awt.*;
import javax.swing.*;

public class Java_2{
    
    
    int grades[][] = {
    
     {
    
     77, 68, 86, 73 },
                        {
    
     96, 87, 89, 81 },
                        {
    
     70, 90, 86, 81 } };
    int students, exams;
    String output;
    JTextArea outputArea;
    
    public Java_2(){
    
        
        students = grades.length;
        exams = grades[ 0 ].length;
        
        JFrame f = new JFrame();
        f.setSize(300,300);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //*********Found**********
        outputArea = new JTextArea();
        Container c = f.getContentPane();
        //*********Found**********
        c.add( outputArea );
        
        output = "数组是:";
        buildString();
        output += "\n\n最高分: " + maximum() + "\n";
         //*********Found**********
        for ( int i = 0; i <students ; i++ )
            output += "\n第" + (i+1) + "个学生的平均分是: " +
                    average( grades[ i ] );
         //*********Found**********
        outputArea.setText( output );
    }
    
    
    //找最高分
    public int maximum(){
    
    
        int highGrade = 0;
        for ( int i = 0; i < students; i++ )
            for ( int j = 0; j < exams; j++ )
                if ( grades[ i ][ j ] > highGrade )
                     //*********Found**********
                    highGrade = grades[ i ] [ j ];
        return highGrade;
    }
    //对各组学生确定平均分
    public int average( int setOfGrades[] ){
    
    
        int total = 0;
        for ( int i = 0; i < setOfGrades.length; i++ )
             //*********Found**********
            total += setOfGrades[ i ];
       
        return total /exams;
    }
    //输出格式
    public void buildString(){
    
    
        output += "        ";
        for ( int i = 0; i < exams; i++ )
            output += "[" + i + "]   ";
        for ( int i = 0; i < students; i++ ) {
    
    
            output += "\ngrades[" + i + "]   ";
            for ( int j = 0; j < exams; j++ )
                output += grades[ i ][ j ] + "   ";
        }
    }
    
    public static void main(String[ ]args){
    
    
        new Java_2();
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_49095721/article/details/109215109