gpt4 book ai didi

Java "Reached end of file while parsing"

转载 作者:行者123 更新时间:2023-12-01 10:52:33 27 4
gpt4 key购买 nike

我对编程并不陌生,但由于某种原因我无法解决这个问题。我正在编写一个方法,并不断收到“解析时已到达文件末尾”编译器错误。通常,当您忘记 } 时,就会发生这种情况,但此方法只有两组括号,而且我不会错过任何一个括号。谁能指出为什么我会收到此错误?

 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;
}

最佳答案

文件末尾缺少右大括号 }

    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

关于Java "Reached end of file while parsing",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33762602/

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