此时的目标只是找出为什么不编译此代码。下面的类根据某些条件创建“ K12Student”的新实例,该类是一个超级类,它扩展到定义不同类型学生的三个子类。每个子类包含唯一的实例变量及其get和set方法。当前的类如下:

import java.util.*;

//Create New arraylist for client instances
public class StudentInput {

    private  InputHelper input;
    private  ArrayList students;


    public void run() {
        studentInfoEntry();
    }

//Assign data to instances of client
    public void studentInfoEntry() {

        students = new ArrayList();
        input = new InputHelper();

        String studentIDString = "";
        int studentID = 0;
        String studentName = "";
        String schoolName = "";
        String gradeLevelString = "";
        int gradeLevel = 0;
        String validateAddNewStudent = "";

        while (true) {
            studentIDString = input.getUserInput("Enter student ID number.");
            studentID = Integer.parseInt(studentIDString);
            studentName = input.getUserInput("Enter student name.");
            schoolName = input.getUserInput("Enter school name.");
            gradeLevelString = input.getUserInput("Enter grade level.");
            gradeLevel = Integer.parseInt(gradeLevelString);

            if (gradeLevel >= 0 && gradeLevel <= 12) {

                if (gradeLevel >= 0 && gradeLevel <= 4) {

                    String readingLevelString = "";
                    int readingLevel = 0;
                    String classSection = "";

                    readingLevelString = input.getUserInput("Enter reading level.");
                    readingLevel = Integer.parseInt(readingLevelString);
                    classSection = input.getUserInput("Enter class section.");

                    /*K12Student*/
                    PrimaryStudent newStudent = new PrimaryStudent();

                    newStudent.setStudentID(studentID);
                    newStudent.setStudentName(studentName);
                    newStudent.setSchoolName(schoolName);
                    newStudent.setReadingLevel(readingLevel);
                    newStudent.setClassSection(classSection);
                }

                if (gradeLevel >= 5 && gradeLevel <= 8) {

                    String lunchHourString = "";
                    int lunchHour = 0;
                    String homeroomTeacher = "";

                    lunchHourString = input.getUserInput("Enter lunch hour.");
                    lunchHour = Integer.parseInt(lunchHourString);
                    homeroomTeacher = input.getUserInput("Enter homeroom teacher.");

                    /*K12Student*/
                    MiddleStudent newStudent = new MiddleStudent();

                    newStudent.setStudentID(studentID);
                    newStudent.setStudentName(studentName);
                    newStudent.setSchoolName(schoolName);
                    newStudent.setLunchHour(lunchHour);
                    newStudent.setHomeroomTeacher(homeroomTeacher);
                }

                if (gradeLevel >= 9 && gradeLevel <= 12) {

                    String GPAString = "";
                    int GPA = 0;
                    String collegeChoice = "";

                    GPAString = input.getUserInput("Enter reading level.");
                    GPA = Integer.parseInt(GPAString);
                    collegeChoice = input.getUserInput("Enter class section.");

                    K12Student newStudent = new HighStudent();

                    newStudent.setStudentID(studentID);
                    newStudent.setStudentName(studentName);
                    newStudent.setSchoolName(schoolName);
                    newStudent.setReadingLevel(readingLevel);
                    newStudent.setClassSection(classSection);
                }
                students.add(newStudent);

                validateAddNewStudent = input.getUserInput("Enter another student? (y/n)");

                if (!validateAddNewStudent.equals("y")) {
                    break;
                }

            }else{
                System.out.println("Grade level must be from 0-12.");
            }
        }
    }
}


同样,每个新的初中和小学生的实例化实例最初的读取方式都与高中生(K12Student newStudent = new HighStudent;)的实例化方式相同,但是在使用该格式进行编译时,我遇到了更多的问题。编译器错误如下:

javac K12StudentTestDrive.java

./StudentInput.java:97: error: cannot find symbol
                    newStudent.setReadingLevel(readingLevel);
                                               ^
  symbol:   variable readingLevel
  location: class StudentInput
./StudentInput.java:98: error: cannot find symbol
                    newStudent.setClassSection(classSection);
                                               ^
  symbol:   variable classSection
  location: class StudentInput
./StudentInput.java:100: error: cannot find symbol
                students.add(newStudent);
                             ^
  symbol:   variable newStudent
  location: class StudentInput
3 errors


预先感谢您的任何反馈。

最佳答案

变量仅存在于声明它们的作用域中,作用域在大多数情况下是花括号(还有其他作用域规则,但在您的情况下,它是块作用域)。您可以在较早的if语句的花括号内定义变量。一旦代码离开该if语句,它们就会超出范围。以后将不可用。

您的代码的重要部分是:

if (...) {
    int readingLevel = ...;
    String classSection = ...;
    ...
} // <= readingLevel and classSection go out of scope here

if (...) {
    K12Student newStudent = ...;
    newStudent.setReadingLevel(readingLevel); // <= readingLevel isn't here
    newStudent.setClassSection(classSection); // <= classSection isn't here
} // <= newStudent goes out of scope here

students.add(newStudent); // <= newStudent isn't here


注意,这与抽象类无关。

10-07 22:34