這不應該是一個客戶端類。我只是在爲其他人使用課程。我使用這個爲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