【问题标题】:Beginner Java (getting class assignment to compile)初学者 Java(获取要编译的类分配)
【发布时间】:2009-08-17 22:14:02
【问题描述】:

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

【问题讨论】:

  • 我没有看到你宣布学生..?
  • 打印编译器的输出,会有用的
  • SO 上一半或更多的人会不同意,但它应该在您编写它时一直在编译。一个好的 IDE(比如 Eclipse 或 Netbeans)会告诉你语法错误在哪里。如果您被允许并且有时间,请使用一些工具。我使用文本编辑器学习了 Java,这非常浪费时间。
  • 等等,这就是现在人们完成 CS 实验室任务的方式吗?堆栈溢出?我担心这个行业。

标签: java file


【解决方案1】:

首先:真正注意编译器错误总是有用的。 Java 的编译器错误在大多数情况下都非常清晰和有用,一旦你学会了理解它们,它们就会准确地告诉你哪里出了问题。通常,如果您包含错误的实际文本而不是说“我无法将它送到编译器”,那么 SO 上的人们会更容易帮助您

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

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

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

  • 变量的类型
  • 它的名字
  • 可选的初始值赋值

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

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

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

【讨论】:

  • 我刚刚输入了相同的答案
  • 也被称为“西部最快的枪”问题:)
  • 显然这并不明显。 ;-)
【解决方案2】:

数组的大小不能在左侧指定。

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

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

【讨论】:

    【解决方案3】:

    这部分看起来是个问题:

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

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

    Student[] students = .....
    

    【讨论】:

      【解决方案4】:

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

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

      我总是建议以与编译器相同的心态阅读有问题的行:除了你告诉它的内容,它什么都不知道,从上到下,从左到右。

      让我们从头开始:

      int
      

      我知道int 是什么!你要申报一个!惊人的!继续……

      Student
      

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

      更重要的是,如果它是一个类,那么您刚刚连续命名了两个数据类型:intStudent。您打算使用哪种类型?大概如果您想要Students 的列表,int 根本不相关。如果你想要 number 个学生,那么 just int 是相关的。

      继续:

      students
      

      students 到底是什么?我再次在代码中没有看到 anywhere 可以告诉我那是什么!

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

      但是,如果您还没有阅读数组,您应该如何知道该数组有多大?方便,你不必知道!你可以简单地写:

      Student[]
      

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

      好的,那你可以写吗:

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

      不,因为你的变量没有名字。像这样的东西怎么样:

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

      我怀疑这就是您的本意,但由于您在此过程中被卡在某个地方,因此能够“解开卡住”很重要,并且在心理上通过编译器看到的内容是一种有用的方法。

      【讨论】:

      • 这仅适用于强类型语言。
      • 嗯...沿着“像编译器一样思考”这一行:heck 是什么“int Student[]”??
      • 我想,使用正确的语言会有所帮助。相应地进行了编辑。
      • "int[] Student students" 仍然不是有效的语法。 int 不属于其中(在 Java 中允许将数组括号与名称放在一起,但不赞成)。不过,+1 是为了逐步介绍编译器的思维方式。
      • 哇。这显然是一天的结束,因为我完全错过了。即将进行大量编辑。
      猜你喜欢
      • 2016-03-08
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 2020-08-08
      相关资源
      最近更新 更多