我正在尝试使用二维数组创建一个程序来存储有关学生的信息,他们的名字和姓氏,然后是4个等级。
用户必须能够将所有这些输入到程序中,然后按一个按钮来计算课程的平均值。他们还应该能够输入学生的名字和姓氏,并输出他们的平均成绩。该程序还应该能够列出存储在其中的所有信息。
我意识到使用arraylists可能会更容易,但这是一个课程,它指定我必须使用二维数组。
我们使用的开发环境是NetBeans 6.5(6.8是保留设计窗口的最后一个版本)。是的,我意识到这已经过时了,但这就是我们必须使用的东西。这个版本有一个设计窗口,所以我们不必担心任何GUI编码,我们只需拖动我们需要的标签和文本区域和按钮。它必须能够处理多达15名学生。我会尝试解释代码背后的想法和理论。
//Create and intialize array to have 15 rows and 6 columns.
String [][] records = new String [15][6];
int n = 1;
double clicked = 0;
private void addActionPerformed(java.awt.event.ActionEvent evt) {
//The basic idea behind the add method here is that we take the input from the user and assign it to the array by taking the number of times this method has been used as a reference for where it should go in the array. This method actually works fine, I just put in here for reference in case someone thinks it might be important.
String nameFirst,nameLast, grade1,grade2, grade3, grade4;
nameFirst = firstName.getText();
nameLast = lastName.getText();
grade1 = class1Grade.getText();
grade2 = class2Grade.getText();
grade3 = class3Grade.getText();
grade4 = class4Grade.getText();
records[n-1][0] = nameFirst;
records[n-1][1] = nameLast;
records[n-1][2] = grade1;
records[n-1][3] = grade2;
records[n-1][4] = grade3;
records[n-1][5] = grade4;
output.append(records[n-1][0] + "\n");
output.append(records[n-1][1] + "\n");
output.append(records[n-1][2] + "\n");
output.append(records[n-1][3] + "\n");
output.append(records[n-1][4] + "\n");
output.append(records[n-1][5] + "\n");
n++;
firstName.setText("");
lastName.setText("");
class1Grade.setText("");
class2Grade.setText("");
class3Grade.setText("");
class4Grade.setText("");
clicked++;
}
private void listActionPerformed(java.awt.event.ActionEvent evt) {
//This is the code that we were shown to display all elements in the array but for whatever reason it doesn't want to go past row 0(first row) and I don't know why.
for (int row = 0; row <= 16; row++) {
output.append(row + "\n");
for( int col=0; col <= 6; col++) {
output.append(records[row][col] +"\n");
}
}
//This is the code I added in to make sure that the add method was actually putting things where they belonged. It is but it did show that the above loop wasn't working.
//output.append(records[0][0] + "\n");
//output.append(records[0][1] + "\n");
//output.append(records[0][2] + "\n");
//output.append(records[0][3] + "\n");
//output.append(records[0][4] + "\n");
//output.append(records[0][5] + "\n");
//output.append(records[1][0] + "\n");
//output.append(records[1][1] + "\n");
//output.append(records[1][2] + "\n");
//output.append(records[1][3] + "\n");
//output.append(records[1][4] + "\n");
//output.append(records[1][5] + "\n");
}
private void calculateCourseAverageActionPerformed(java.awt.event.ActionEvent evt) {
double total1 = 0, total2 = 0, total3 = 0, total4 =0;
double col2, col3, col4, col5;
//col2 = Double.parseDouble(records[0][2]);
//Here I try to add up all the stored values in a particular column. Originally it kept giving back a floating decimal error. It turned out the the value in the array was being multiplied by 64(4 from first loop, 16 from second, 16*4=64). Now its only being multiplied by 16, not much better but an improvement none the less.
for(int col = 2; col<=5; col++){
for(int row = 0; row<=15; row++){
if(col == 2){
col2 = Double.parseDouble(records[0][2]);
total1 = total1 + col2;
}
if(col == 3){
col3 = Double.parseDouble(records[0][3]);
total2 = total2 + col3;
}
if(col == 4){
col4 = Double.parseDouble(records[0][4]);
total3 = total3 + col4;
}
if(col == 5){
col5 = Double.parseDouble(records[0][5]);
total4 = total4 + col5;}
}
}
output.append("The average for course 1 is " + total1/4);
output.append("The average for course 2 is " + total2/4);
output.append("The average for course 3 is " + total3/4);
output.append("The average for course 4 is " + total4/4);
}
private void calculateStudentAverageActionPerformed(java.awt.event.ActionEvent evt) {
String nameFirst, nameLast;
double studentAverage, total = 0, grade, finalGrade;
boolean continuation;
int student = 0;
//here we take the first name and last name(that the user should input). The method is supposed to check through the array for the first name and if it finds it it should check the last name then add up all the number is that particular row. Tests show that it seems to work fine until the Name1 Name2 area(*), it shows that the records[row][0] and records[row][1] are equal to null which negates the rest of my code here.
nameFirst = firstName.getText();
nameLast = lastName.getText();
output.append("Name check " + nameFirst + nameLast + "\n");
while(continuation = true){
for(int row=0;row<=15;row++){
for(int col=0;col<=0;col++){
*
output.append("Name1 " + records[row][0] + "\n");
output.append("Name2 " + records[row][1] + "\n");
*
if(records[row][0].equals(nameFirst)){
if(records[row][1].equals(nameLast)){
continuation = false;
student = row;
String test = Integer.toString(student);
output.append("Student row # " + test + "\n");
}
}
}
}
}
for(int col = 2; col<=5; col++){
grade = Double.parseDouble(records[student][col]);
output.append("Grade " + grade + "\n");
total = total + grade;
output.append("Total " + total + "\n");
}
finalGrade = total / 4;
output.append(finalGrade + "\n");
output.append("First Name " + records[student][0] + "\n");
output.append("Last Name " + records[student][1] + "\n");
output.append(records[student][0] + records[student][1] + "'s average is " + finalGrade);
continuation = true;
}
任何帮助或想法,想法等......都将非常感激。在过去的一个月里,我一直坚持这个和匹配的游戏任务,我想是时候我要求别人提供意见和建议。
答案 0 :(得分:0)
您必须将总数除以15而不是4 ...
output.append("The average for course 1 is " + total1/15);
output.append("The average for course 2 is " + total2/15);
output.append("The average for course 3 is " + total3/15);
output.append("The average for course 4 is " + total4/15);
以下行是相同的方法是16次而不是15次,这将在您的情况下抛出空指针异常...更改
for(int row = 0; row<=15; row++){
到
for(int row = 0; row<15; row++){
以下代码对您来说非常合适...
double[] totals = new double[4];
for(int row = 0; row<=15; row++){
for(int col = 2; col<=5; col++){
total[col - 2] = total[col - 2] + records[row][col];
}
}
}
答案 1 :(得分:0)
for (int row = 0; row <= 16; row++) {
所以当row = 16
遇到这一行时:
output.append(records[row][col] +"\n");
根据您的声明,可能会将NullPointerException
作为索引15
而16
将不存在(索引将为0到14,因为您需要15行)。
此行for (int col = 0; col <= 6; col++) {
所以请尝试将其更改为:
for (int row = 0; row < 15; row++) {
output.append(row + "\n");
for( int col=0; col < 6; col++) {
output.append(records[row][col] +"\n");
}
}