Я не в порядке с этим кодом, это просто шаблон, и я еще не закончил форматирование моего кода. У меня возникли проблемы с вызовом части сообщения массива в моем другом классе. У меня в основном есть оператор if/else, говорящий, что roomId = some #, он будет распечатывать сообщение, которое коррелирует с # из массива. У меня просто возникает проблема с тем, как вызвать массив. Eclipse бросает мне ошибку в выражении if/else в разделе «сетка», поскольку сетка не может быть разрешена переменной. Я также попытался вызвать метод массива внутри метода, в котором содержатся инструкции. Спасибо за помощь ребятам.Вызов двумерного массива
public class location{
public int roomId;
public String name, message;
public location() {
roomId = 0;
}
public location(String name, int roomId, String message){
this.name = name;
this.roomId = roomId;
this.message = message;
}
public void LocArray() {
location[][] grid = new location[4][4];
grid [1][0] = new location("LABORATORY", 0, "You're in the lab.");
grid [2][0] = new location("DUNGEON", 1, "You entered the dungeon.");
grid [3][0] = new location("COURTYARD ENTRANCE",2,"You have left the dungeon out the backdoor. Either head east and search the courtyard maze, or travel north back to the dungeon");
grid [3][1] = new location("FIRST PATH", 3,"You have now entered the courtyard, either continue east or move north.");
grid [3][2] = new location("DEADEND", 4,"You have reached a deadend that has a Magic Shop. Go inside and explore it.");
grid [3][3] = new location ("MAGIC SHOP", 5, "Search around the Magic Shop and see what there is. When you're done searching continue through the maze.");
grid [2][1] = new location("SECOND PATH",6,"Search the surroundings for items that will help you get into the locked room, then keep moving.");
grid [2][2] = new location("END MAZE", 7, "You've made it to the end of the courtyard. There seems to be a cave in the distance; go check it out.");
grid [1][2] = new location("CAVE",8,"Explore the cave to find the remaining items that will lead to your freedom.");
grid [0][0] = new location("EXIT",9,"This room will lead to your freedom, but you need the three essential items that will open this door.");
}
}
переменная
//This is a different class called projectTwo.
while (stillPlaying) {
Scanner scan = new Scanner(System.in);
userInput = scan.nextLine();
if (roomId == 0){
if (userInput.equals("n")) {
System.out.println(grid[2][0].message);
roomId = 1; // Moves user from location 0 to 1
}
«Я еще не закончил форматирование своего кода». Это намного проще, если вы делаете это, когда идете :) – Jon
'grid' - локальная переменная в' LocArray() '. Вы не можете ссылаться на него нигде. –
В более серьезной заметке вы указали 'grid' внутри' LocArray() 'в' location', так что вы не можете получить к ней доступ нигде. Сделайте его переменной-членом ('public location [] [] grid;' вверху вверху), а затем вызовите его с помощью 'location.grid'. – Jon