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

时间:2009-08-17 22:14:02

标签: java file

这是我为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);
        }
}

4 个答案:

答案 0 :(得分:5)

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

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

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

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

组成
  • 变量的类型
  • 其名称
  • 可选择分配初始值

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

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

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

答案 1 :(得分:3)

无法在左侧指定数组的大小。

学生数组声明应如下所示:

int noOfStudents = FileIOHelper.getNumberOfStudents(filename);
//create an array of students of the given length
Student[] students = new Student[noOfStudents]; 

答案 2 :(得分:1)

这部分看起来是一个问题:

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

您可能想先获取学生人数,然后使用该变量调用getStudents。此外,如果您希望将数组称为学生,则不应将其放在括号中。

Student[] students = .....

答案 3 :(得分:1)

您遇到问题的行是main()顶部的第一行:

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

我总是建议通过与编译器具有相同心态的有问题的行来阅读:除了你所说的,从上到下,从左到右,它什么都不知道。

所以让我们从头开始:

int

我知道int是什么!你想宣布一个!真棒!继续......

Student

Student到底是什么?我没有在代码中看到 where ,它会告诉我那是什么!作为一个人,我可以推断它应该是一个类的名称,因为类名总是在Java中大写,但它没有被声明,因此编译器无法确定。它可能应该是这个类的名称(而不是Lab7)吗?

更重要的是,如果它是一个类,那么您只需要连续命名两个数据类型:intStudent。您打算使用哪种类型?假设您想要Student的列表,int根本不相关。如果您想要的学生,那么 int是相关的。

继续前进:

students

students到底是什么?我再次在代码中看不到 where ,它会告诉我那是什么!

让我们退一步。你真的想在这做什么?您正在尝试获取文件中所有学生的数组。 getStudents()函数可能实现了这一点。 (这可能是错误的,但目前这不是我们的问题。我们只是在这里调用它,所以我们假设它有效。)

但是如果你还没有阅读它,你怎么知道制作阵列有多大?方便的是,你不必知道!你可以简单地写一下:

Student[]

您正在getStudents()实例化数组,并且您已正确地给它一个大小。在main()中,您只是声明它,并且不需要任何大小。

好的,你也可以这样写:

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

不,'因为你的变量没有名字。这样的事情怎么样:

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

我怀疑这就是你想要的,但是既然你被困在某个地方,重要的是能够“解开”,并且在精神上走过编译器所看到的是一种有用的方法。