我对编程并不陌生,但出于某种原因,我无法解决这个问题。我正在编写一个方法,并继续获得“解析时达到文件末尾”编译器错误。通常这会在你忘记一个}时发生,但是这个方法只有两组括号,我也不会错过任何一个。谁能指出为什么我会收到这个错误?
public class Locations{
//member variables
static int totalNumberOfRooms = 0;
int numberOfExits;
//pointers to each exit
String roomGeneralDescription;
String roomDescription;
//member methods
String getRoomGeneralDescription(){
return this.roomGeneralDescription;
}
String getRoomDescription(){
return this.roomDescription;
}
//constructor to more easily create objects
public Locations(int exit, String description, String generalDescription){
totalNumberOfRooms += 1;
numberOfExits = exit;
roomDescription = description;
roomGeneralDescription = generalDescription;
}
//default constuctor
public Locations(){
totalNumberOfRooms += 1;
}
//generates the given number of Locations obejcts, with pointers stored in a returned
//array.
Locations[] createLocations(int x){
int iterate = 1;
int loopMax = x;
Locations[] arrayOfLocations = new Locations[x -1];
while (iterate <= loopMax){
int index = iterate -1;
arrayOfLocations[index] = new Locations();
iterate += 1;
}
return arrayOfLocations;
}
答案 0 :(得分:1)
您在文件末尾缺少右括号}
。
Locations[] createLocations(int x){
int iterate = 1;
int loopMax = x;
Locations[] arrayOfLocations = new Locations[x -1];
while (iterate <= loopMax){
int index = iterate -1;
arrayOfLocations[index] = new Locations();
iterate += 1;
}
return arrayOfLocations;
}
} // YOU NEED TO ADD A CLOSING BRACE TO FINALIZE THE CLASS DEFINITION
更新:即使结束括号是解决方案,我也不禁注意到你的createLocations
方法是如何写的。这是一种奇怪的数组分配方式。我甚至不确定你的内容会在没有崩溃的情况下运行,因为数组的大小为[x-1]
。无论如何,这是一个用Java创建数组的更简洁的解决方案。我希望这有帮助!
Locations [] createLocations(int count) {
Locations [] arrayOfLocations = new Locations[count];
for (int i = 0; i < count; i++) {
arrayOfLocations[i] = new Locations();
}
return arrayOfLocations;
}
} // YOU NEED TO ADD A CLOSING BRACE TO FINALIZE THE CLASS DEFINITION