如何检查用户输入是否与二维数组的元素匹配? JAVA

How do I check to see if a user Input matches an element of a 2D array? JAVA

我正在创建一个学生成绩簿,允许用户输入他们的学生姓名和 4 个成绩,然后可以选择计算学生和 class 平均分,如下图所示。

使用下面的代码,我收到的学生平均成绩为“0.0%”并且没有错误。我是否正确地指示程序查看学生姓名是否与二维数组中的适当元素相匹配?

程序的所有其他部分都在运行。

private String[][] grades = new String[16][5];

//Declare Variables
String firstName, lastName, name;
String test1, test2, test3, test4;

test1 = t1Input.getText();
test2 = t2Input.getText();
test3 = t3Input.getText();
test4 = t4Input.getText();

firstName = firstInput.getText();
lastName = lastInput.getText();
name = firstName + " " + lastName + ": ";

grades[students][0] = name;
grades[students][1] = test1;
grades[students][2] = test2;
grades[students][3] = test3;
grades[students][4] = test4;

students++;
private void sAvgInputActionPerformed(java.awt.event.ActionEvent evt) {
    String firstName = firstInput.getText();
    String lastName = lastInput.getText();
    String name = firstName + " " + lastName + ": ";
    int sum = 0;
    double divide = 0;

    for (int i = 0; i <= 1; i++) {
        if (name.equals(grades[0][1])) {
            for (int grade = 1; grade <= 4; grade++) {
                sum = Integer.parseInt(grades[i][1]) + i;
            }
            break;
        }
    }
    divide = sum / 4;
    gradesOutput.setText(firstName + "'s grade average is " + divide + "%.");
}

将循环更改为:

//assume it as a simple table since its 2d array
//Since you are storing the name in first(0th index) column,
//only row should be loop
for (int i = 0; grades[i][0] != null; i++) {
    if (name.equals(grades[i][0])) {
        for (int grade = 1; grade <= 4; grade++)
            //same row,different column
            sum = sum + Integer.parseInt(grades[i][grade]);
        break;
    }
}