我应该在哪里声明该代码中的字段才能编译?
问题描述:
这不应该是一个客户端类。我只是在为其他人使用课程。我使用这个为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;
}
答
此外,你写的东西,不从查看Java编译器的点意义:
private String period;) {
- 可能删除“)”。
的第二件事:
以阶级Classcourse的声明一起来看看。 它听起来不对,虽然它可能是本网站的编辑器或什么的问题...
“整体”的提示 - java在大多数情况下,它可以说出什么是错误的“智能”编译器与你的代码完全一样,假设你是Java中的新手,试着理解编译器对你说的话。
祝你好运!
+0
';)'这是经典 - 有人习惯于打字表情符号! –
答
有些事情我注意到了有关的代码:
public String getperiod() {
return getperiod();
}
此代码将导致一个死循环,当你调用这个函数。
private String address;) {
this.students = students;
}
编译器会给出关于“;)”的错误。将其更改为“()”以解决此问题。
此外,你应该告诉我们更多关于它给你的错误。如果您不给我们编译器错误,我们无法帮助您。
编译器给你什么错误信息? – Gnat
_这是一个提示_:你应该声明编译器所说的**不能**的字段。编译器会告诉你它找不到哪一个!这行看起来很奇怪,在字母'a'和'd'之间留有空格:'this.String address = a ddress;'删除_white space_。 – CoolBeans
_总是包含你的编译器错误信息。这是足够的,告诉你到底什么不喜欢你的输入。你最终会学会如何理解编译器试图告诉你什么。如果你包含错误信息,这个过程将变得更快,然后_we_可以用简单的英语告诉你(或者接近简单的......)真正的问题是什么。然后你学习。 :) – sarnold