gpt4 book ai didi

java - 找不到标志

转载 作者:行者123 更新时间:2023-12-01 13:07:27 24 4
gpt4 key购买 nike

此时的目标只是找出这段代码无法编译的原因。下面的类根据特定条件创建“K12Student”的新实例,该类是一个父类(super class),它扩展到定义不同类型学生的三个子类。每个子类都包含唯一的实例变量及其 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

预先感谢您的任何反馈。

最佳答案

变量仅存在于它们声明的范围内,范围在很大程度上是大括号(还有其他范围规则,但在您的情况下,它是 block 范围)。您可以在前面的 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

请注意,这与抽象类无关。

关于java - 找不到标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23164861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com