我的程序存在一些问题,因为它无法计算每次考试的平均成绩,也无法计算学生的平均成绩和成绩。
有人可以帮助我更正我的代码吗?因为我需要在周三上交作业,还可以帮助我检查那里的其他代码是否有错误。
非常感谢。
import java.util.*;
import java.io.*;
public class Assignment {
public static void main(String[] args) throws IOException {
// Variables for input name and output name
String nameInp = "";
String nameOut = "";
// Arrays to store marks of 5 tests of each student
double [] tests = new double[5];
// Arrays to store average and sum of the 5 tests
double[] testAvg = new double[5];
double[] testSum = new double[5];
// Arrays to store max and min of the 5 tests
double[] max = new double[5];
double[] min = new double[5];
// Arrays to store the name of students who got max or min
// in the 5 tests
String[] nameMax = new String[5];
String[] nameMin = new String[5];
// Number of students
int studCount = 0;
// Average and grade of the 5 tests of each students
double studAverage;
String studGrade;
// Scanner for reading file
Scanner scf = new Scanner(new File(System.getProperty("user.dir") +
"/src/student_marks.txt"));
// Set the max and min of the 5 tests
// to Double's min and max respectively
double max_tests = Double.MAX_VALUE;
double min_tests = Double.MIN_VALUE;
// Print output heading
System.out.println("Student Name Test1 Test2 Test3 Test4 Test5 Average Grade");
System.out.println("---------------------------------------------------------");
// While there are more data to read from the input file
while (scf.hasNext()) {
// Increment number of students
int count;
count++;
// Read a student name
nameInp = scf.next();
// Replace the "_" in the name with a " "
nameOut = nameInp.replaceAll("_", " ");
// For each student, calculate the sum of the test marks,
// record the max and min marks of each test and
tests[5] = scf.nextDouble();
testSum[5] += tests[5];
if (tests[5] > max_tests) {
max_tests = tests[5];
}
if (tests[5] < min_tests) {
min_tests = tests[5];
}
// record the name of studunts scoring those max and min marks
if (tests[5] > max_tests) {
max_tests = tests[5];
nameMax[5] = nameOut;
}
if (tests[5] < min_tests) {
min_tests = tests[5];
nameMin[5] = nameOut;
}
// Call the computeStudAvg method to calculate the average marks
// of each student
studAverage = computeStudAvg( testAvg );
// Call the computeStudGrade to get the grade of each student
studGrade = computeStudGrade( );
// Print the tests, average and grade of each student
System.out.printf("%-10s %6.2f %6.2f %6.2f\n", nameOut, tests[1], tests[2], tests[3], tests[4], tests[5], studGrade);
} // end while more students
// Calculate the average marks of each of the 5 tests
testAvg[5] = testSum[5] / 5;
// Print the average of each test
System.out.printf("Test Average: ", testAvg[1], testAvg[2], testAvg[3], testAvg[4], testAvg[5]);
// Print the max marks, min marks and names of students
System.out.println("Test Max--Student Min-Student ");
System.out.println("------------------------------------------------------------------");
System.out.printf("Test1" + max[1] , nameMax[1] , min[1] , nameMin[1]);
System.out.printf("Test2" + max[2] , nameMax[2] , min[2] , nameMin[2]);
System.out.printf("Test3" + max[3] , nameMax[3] , min[3] , nameMin[3]);
System.out.printf("Test4" + max[4] , nameMax[4] , min[4] , nameMin[4]);
System.out.printf("Test5" + max[5] , nameMax[5] , min[5] , nameMin[5]);
System.out.println();
scf.close();
}
// Method to compute and return the average mark of the 5 tests for each student
public static double computeStudAvg(double[] testSum) {
return (testSum[5] / 5 );
}
// Method to compute and return the grade of each student
public static String computeStudGrade(double[] studAvg ) {
if ( average >=75 ) {
return "A";
} else if ( average >= 65 ) {
return "B";
} else if ( average >= 50 ) {
return "C";
} else if ( average >= 40 ) {
return "D";
} else
return "F";
}
}
答案 0 :(得分:0)
您的问题之一是计算最小值和最大值。
反转这些行中的分配:
double max_tests = Double.MAX_VALUE;
double min_tests = Double.MIN_VALUE;
应为:
double max_tests = Double.MIN_VALUE;
double min_tests = Double.MAX_VALUE;
如果您将分数与min_tests
进行比较,如果其值为Double.MIN_VALUE
,则比较将总是失败,因为没有任何分数低于Double.MIN_VALUE
。