如何从Java的二维对象数组中提取数字



我有一个二维对象数组,其中充满了学生姓名和成绩。每个学生都有自己的行,第一列包含学生姓名,然后接下来的4列包含考试成绩。我需要找到每个学生测试的平均值,但我不知道如何平均每一行的双值。我还需要在数据库中找到所有测试的平均值。有人能告诉我怎么做吗?

public class StudentGrades extends javax.swing.JFrame {
    Object[][] database = new Object[14][5];
    int studentCount = 0;

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                               
    //declare variables
    double grade1 = Double.parseDouble(test1.getText());
    double grade2 = Double.parseDouble(test2.getText());
    double grade3 = Double.parseDouble(test3.getText());
    double grade4 = Double.parseDouble(test4.getText());
    String name = firstInput.getText() + " " + lastInput.getText() + ":";
    if (grade1 >= 0 & grade1 <= 100 & grade2 >= 0 & grade2 <= 100 & grade3   >= 0 & grade3 <= 100 & grade4 >= 0 & grade4 <= 100){
        //add data
        database[studentCount][0] = name;
        database[studentCount][1] = grade1; 
        database[studentCount][2] = grade2;
        database[studentCount][3] = grade3;
        database[studentCount][4] = grade4;
        //add to student count
        studentCount = studentCount + 1;
        //limit number of students
        if (studentCount == 15){
            addButton.setEnabled(false);
        }
    }  
    else {
        errorLabel.setText("Please Enter Valid Data.");
    }

一个合理的Student类设计的例子可能是这样的(实现留空):

import java.util.List;
import java.util.ArrayList;
public class Student {
    private String name;
    private List<Double> grades;
    public Student(String name) {
        // TODO 
    }
    public void addGrade(double grade) {
        // TODO
    }
    public double average() {
        // TODO 
    }
    public String toString() {
        return String.format(
            "Student: %snAverage Grade: %fn", 
            this.name, 
            this.average()
        );
    }
}
class TestStudent {
    public static void main(String[] args) {
        Student s = new Student("Hunter");
        s.addGrade(100);
        s.addGrade(90);
        s.addGrade(0);
        s.addGrade(85);
        s.addGrade(75);
        System.out.println(s);
    }
}

使用当前代码:

Object[][] database = new Object[14][5];

我们可以添加一些东西使它对读者更友好

final int STUDENTS = 14;
final int GRADES = 4;
Object[][] database = new Object[STUDENTS][GRADES + 1];

为了能够检查平均值

public double getStudentAverageGrade(Object[] student)
{
    for(int i = 0; i < GRADES; i++) // forEach grade
    {
        gradesSummatory += (double) student[i + 1]; // plus one is added since we ignore first value (which is students name)
    }
    return gradesSummatory/GRADES;
}

现在,为了能够看到所有学生的平均成绩,这更容易,因为我们只需要实现前面创建的方法。

public double getAverageGrade()
{
    double avgSummatory = 0;
    for (int i = 0; i < STUDENTS; i++)
    {
        avgSummatory += getStudentAverageGrade(database[i]);
    }
    return avgSummatory/STUDENTS;
}

我不认为这是一个真正的最佳解决方案(你可以把所有元素加起来,然后只除以一次),但它是最有表现力的非面向对象的解决方案。我宁愿将这些元素/对象抽象到不同的类中,例如Student,以便尽可能地重用代码。但对于这种情况,这可以达到目的。

相关内容

  • 没有找到相关文章

最新更新