这不应该是一个客户端类。我只是在为其他人使用课程。我使用这个为Highschool
。例如,我有address
,teacher
,students
,principal
,roomnumber
等的类。但它没有编译出于某种奇怪的原因。我相信它是因为我没有宣布领域,但不确定。我应该在哪里声明该代码中的字段才能编译?
import java.io.*;
public class HighSchool {
// Constructors
public HighSchool() { }
public HighSchool(String title, String teacher, int roomNumber, String period, String[] students, String address, String subjects) {
this.title = title;
this.teacher = teacher;
this.roomNumber = roomNumber;
this.period = period;
this.String[] students = students;
this.String address =a ddress;
this.String subjects = subjects;
}
public class Classcourse (String title, String teacher, int roomNumber, String period, String[] students, String address, String subjects
private String period;) {
public String gettitle() {
return title;
}
public void settitle(String title) {
this.title = title;
}
public String getteacher() {
return teacher;
}
public void setteacher(String teacher) {
this.teacher = teacher;
}
public int getroomNumber() {
return roomNumber;
}
public void setroomNumber (int roomNumber) {
this.roomNumber = roomNumber;
}
public String getperiod() {
return getperiod();
}
public void setperiod (String period) {
this.period = period;
}
public String[] getstudents() {
return students[];
}
public void setstudents[] (String[] students
private String address;) {
this.students = students;
}
public String getaddress() {
return address;
}
public void setaddress (String address) {
this.address = address;
}
public String getsubjects() {
return subjects;
}
public void setsubjects (String subjects) {
this.subjects = subjects;
}
}
// modifier method
public void addstudents(String students) {
String[] newstudents = new String[students.length + 1];
for (int i = 0; i < students.length; i++) {
newstudents[i] = students[i];
}
newstudents[students.length] = student;
students = newstudents;
}
public boolean isInClass(String students) {
for (int i = 0; i < students.length; i++) {
if (students[i].equals(students)) {
return true;
}
}
return false;
}
// static creator method
public static HighSchool readFromInput() throws IOException {
BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a HighSchool title: ");
HighSchool newHighSchool = new HighSchool(kb.readLine());
String students = null;
do {
System.out.print("Enter a student, or press Enter to finish: ");
students = kb.readLine();
if (students != null){
newHighSchool.addstudents(students);
}
} while (students != null);
return newHighSchool;
}
// Variables (Fields)
private String title;
private String[] students;
}
编译器给你什么错误信息? – Gnat
_这是一个提示_:你应该声明编译器所说的**不能**的字段。编译器会告诉你它找不到哪一个!这行看起来很奇怪,在字母'a'和'd'之间留有空格:'this.String address = a ddress;'删除_white space_。 – CoolBeans
_总是包含你的编译器错误信息。这是足够的,告诉你到底什么不喜欢你的输入。你最终会学会如何理解编译器试图告诉你什么。如果你包含错误信息,这个过程将变得更快,然后_we_可以用简单的英语告诉你(或者接近简单的......)真正的问题是什么。然后你学习。 :) – sarnold