初学者Java(获得编译的类赋值)

1 java file

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

这里可以看到确切的分配.

我在编译时遇到了一些麻烦,尤其是变量" 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);
        }
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*rdt 5

首先:实际关注编译器错误总是有用的.Java的compier错误在大多数情况下非常清晰和有用,并且一旦你学会理解它们就会告诉你到底出了什么问题.一般来说,如果您包含错误的实际文本而不是说"我无法将其转换为编译器",那么SO上的人员可以更轻松地帮助您

这是第一个明显错误的路线:

int Student[students]  = getStudents(FileIOHelper.getNumberOfStudents(filename));
Run Code Online (Sandbox Code Playgroud)

Java中的变量声明包含(略微简化):

  • 变量的类型
  • 其名称
  • 可选地,分配初始值

在上面的行中,你从type开始int,但下一部分Student[students]没有意义 - 它看起来像一个数组实例,当然不是一个名字.你可能意味着:

Student[] students = getStudents(FileIOHelper.getNumberOfStudents(filename));
Run Code Online (Sandbox Code Playgroud)

即类型是Student[](学生对象的数组),名称是"学生",并且它被赋予方法的返回值getStudents().在int任何地方都不涉及(您不必在此指定数组的大小,因为它是在getStudents()方法内创建的).