一切都在正常计算,除了计算平均值只适用于第一个输入(有5个学生年级输入,每个年级3个(
我只是需要帮助解释为什么它似乎只计算一年级。
示例输出:
Jake Smith-考试成绩:67 45 32,平均成绩:48.0,字母成绩:F
Jerry Jones-考试成绩:80 90 100,平均成绩:90.0,字母成绩:
Hanna Davis-考试成绩:88 78 85,平均成绩:83.66666666667,字母成绩:B
我的输出:
Jake Smith-考试成绩:67 45 32,平均成绩:48.0,字母成绩:F
Jerry Jones-考试成绩:80 90 100,平均成绩:138.0,字母成绩:
Hanna Davis-考试成绩:88 78 85,平均成绩:221.66666666666,字母成绩:
import java.util.Scanner;
public class StudentExamAvgLtrGrade {
public static void main(String[] args) {
final int ROWS = 5, COLUMNS = 3;
Scanner scnr = new Scanner(System.in);
String[] studentName = { "Jake Smith", "Jerry Jones", "Hanna Davis", "Betty Lewis", "Elle Garcia" };
double[] numericAverage = new double[ROWS];
char[] gpa = new char[ROWS];
int[][] studentGrade = new int[ROWS][COLUMNS];
double total;
total = 0.0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
studentGrade[i][j] = scnr.nextInt();
total += studentGrade[i][j];
}
numericAverage[i] = total / studentGrade[i].length; //its something about this that seems to be the issue
if(numericAverage[i] < 60) {
gpa[i] = 'F';
} else if(numericAverage[i] < 70) {
gpa[i] = 'D';
} else if(numericAverage[i] < 80) {
gpa[i] = 'C';
} else if(numericAverage[i] < 90) {
gpa[i] = 'B';
} else {
gpa[i] = 'A';
}
}
// Use nested for loops to read in the exam grades (the outside loop is your rows and inside loop is columns)
// Inside the innermost loop, keep a running total of the student's grades
// When the innermost loop completes, compute the average for that student and store it in the numericAverage array
// Hint to compute average:
// numericAverage[i] = total / studentGrade[i].length; // i is the counter in the row loop
// Use the numericAverage value to determine the letter grade and populate the gpa array
// Hint to compute letter grade:
// if (numericAverage[i] < 60) // i is the counter in the row loop
// gpa[i] = 'F';
// else if (numericAverage[i] < 70)
// ...
// Use nested for loops to iterate through the arrays and output array data
// (notice the nested loop to output the exam grades stored in the 2-d array)
for (int i = 0; i < ROWS; i++)
{
System.out.print(studentName[i] + " - Exam Grades:");
for (int j = 0; j < COLUMNS; j++)
System.out.print(" " + studentGrade[i][j]);
System.out.println(", Average: " + numericAverage[i] + ", Letter Grade: " + gpa[i]);
}
}
}
您遇到的问题是:total = 0.0;
你在for
循环之外设置总数,所以它会计算所有学生的所有成绩,所以你应该这样做:
double total;
for (int i = 0; i < ROWS; i++) {
total = 0.0;
for (int j = 0; j < COLUMNS; j++) {
...
}
...
}
每次计算平均值后,应将total
变量重置回0。像这样:
total = 0.0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
studentGrade[i][j] = scnr.nextInt();
total += studentGrade[i][j];
}
numericAverage[i] = total / studentGrade[i].length; //its something about this that seems to be the issue
total = 0; //THIS!!
if(numericAverage[i] < 60) {
gpa[i] = 'F';
} else if(numericAverage[i] < 70) {
gpa[i] = 'D';
} else if(numericAverage[i] < 80) {
gpa[i] = 'C';
} else if(numericAverage[i] < 90) {
gpa[i] = 'B';
} else {
gpa[i] = 'A';
}
}