这是我正在为CSE 201工作的实验室。该程序应从文件中读取有关学生及其分数的信息,并输出每个学生的姓名及其所有分数和总分数,再加上平均分班级分数,以及总分最高和最低的学生的姓名和总分数。

确切的分配可以在here中看到。

我在编译它时遇到了一些麻烦,尤其是使用变量“ students”时。
任何帮助都会很棒。

/*
 * The program will read information about students and their
 * scores from a file, and output the name of each student with
 * all his/her scores and the total score, plus the average score
 * of the class, and the name and total score of the students with
 * the highest and lowest total score.
 */

import java.util.Scanner;

public class Lab7
{
        public static void main(String[] args)
        {
                // Input file name
                Scanner in = new Scanner(System.in);
                String filename = getFileName(in);

                // Input number of students
                int Student[students]  = getStudents(FileIOHelper.getNumberOfStudents(filename));

                // Input all students records and create Student array and
                // integer array for total scores
                int[] totalScores = new int[students.length];
                for(int i = 0; i < students.length; i++){
                        for(int j = 1; j < 4; j++){
                                totalScores[i] += students[i].getScore(j);
                        }
                }

                // Compute total scores and find students with lowest and
        // highest total score
                int maxIndex = 0, minIndex = 0;
                for(int i = 0; i < students.length; i++){
                        if(totalScores[i] > totalScores[maxIndex]){
                                maxIndex = i;
                        }else if(totalScores[i] < totalScores[minIndex]){
                                minIndex = i;
                        }
                }

                // Compute average total score
                int average = 0;
                for(int i = 0; i < totalScores.length; i++){
                        average += totalScores[i];
                }
                average /= students.length;

                // Output results
                outputResults(students, totalScores, maxIndex, minIndex, average);

        }

        // Given a Scanner in, this method prompts the user to enter
        // a file name, inputs it, and returns it.
        private static String getFileName(Scanner in)
        {
                System.out.print("Enter input file name: ");
                return in.nextLine();
        }

        // Given the number of students records n to input, this
        // method creates an array of Student of the appropriate size,
        // reads n student records using the FileIOHelper, and stores
        // them in the array, and finally returns the Student array.
        private static Student[] getStudents(int n)
        {
                Student[] student = new Student[n];
                for(int i = 0; i < student.length; i++){
                        student[i] = FileIOHelper.getNextStudent();

                }
                return student;
        }

        // Given an array of Student records, an array with the total scores,
        // the indices in the arrays of the students with the highest and
        // lowest total scores, and the average total score for the class,
        // this method outputs a table of all the students appropriately
        // formatted, plus the total number of students, the average score
        // of the class, and the name and total score of the students with
        // the highest and lowest total score.
        private static void outputResults(
                        Student[] students, int[] totalScores,
                        int maxIndex, int minIndex, int average
        )
        {
                System.out.println("\nName \t\tScore1 \tScore2 \tScore3 \tTotal");
                System.out.println("--------------------------------------------------------");
                for(int i = 0; i < students.length; i++){
                        outputStudent(students[i], totalScores[i], average);
                        System.out.println();
                }
                System.out.println("--------------------------------------------------------");
                outputNumberOfStudents(students.length);
                outputAverage(average);
                outputMaxStudent(students[maxIndex], totalScores[maxIndex]);
                outputMinStudent(students[minIndex], totalScores[minIndex]);
                System.out.println("--------------------------------------------------------");
        }

        // Given a Student record, the total score for the student,
        // and the average total score for all the students, this method
        // outputs one line in the result table appropriately formatted.
        private static void outputStudent(Student s, int total, int avg)
        {
                System.out.print(s.getName() + "\t");
                for(int i = 1; i < 4; i++){
                        System.out.print(s.getScore(i) + "\t");
                }
                System.out.print(total + "\t");
                if(total < avg){
                        System.out.print("-");
                }else if(total > avg){
                        System.out.print("+");
                }else{
                        System.out.print("=");
                }
        }

        // Given the number of students, this method outputs a message
        // stating what the total number of students in the class is.
        private static void outputNumberOfStudents(int n)
        {
                System.out.println("The total number of students in this class is: \t" + n);
        }

        // Given the average total score of all students, this method
        // outputs a message stating what the average total score of
        // the class is.
        private static void outputAverage(int average)
        {
                System.out.println("The average total score of the class is: \t" + average);
        }

        // Given the Student with highest total score and the student's
        // total score, this method outputs a message stating the name
        // of the student and the highest score.
        private static void outputMaxStudent(
                        Student student,
                        int score
        )
        {
                System.out.println(student.getName() + " got the maximum total score of: \t" + score);
        }

        // Given the Student with lowest total score and the student's
        // total score, this method outputs a message stating the name
        // of the student and the lowest score.
        private static void outputMinStudent(
                        Student student,
                        int score
        )
        {
                System.out.println(student.getName() + " got the minimum total score of: \t" + score);
        }
}

最佳答案

首先:实际注意编译器错误总是很有用的。 Java的编译器错误在大多数时候都是非常清楚和有用的,一旦您学会了理解它们,就可以准确地告诉您出了什么问题。通常,如果您包含错误的实际文本,而不是说“我很难将其提交给编译器”,那么SO上的人员更容易为您提供帮助

这是第一条明显错误的行:

int Student[students]  = getStudents(FileIOHelper.getNumberOfStudents(filename));


Java中的变量声明包括(略有简化):


变量的类型
它的名字
可选地分配初始值


在上一行中,您从类型int开始,但是下一部分Student[students]没有意义-它看起来像数组实例化,当然不是名称。您可能的意思是:

Student[] students = getStudents(FileIOHelper.getNumberOfStudents(filename));


即类型为Student[](学生对象的数组),名称为“学生”,并为其分配了getStudents()方法的返回值。 int不在任何地方使用(您不必在此处指定数组的大小,因为它是在getStudents()方法内部创建的)。

09-30 18:33
查看更多