Java I';我试图从另一个类调用一个二维数组

Java I';我试图从另一个类调用一个二维数组,java,arrays,eclipse,class,methods,Java,Arrays,Eclipse,Class,Methods,我们试图使用数组存储位置,然后在第一个类的if-else语句中调用它们。我们希望能够调用网格位置,这样就不必在if-else语句中键入房间的描述 package ProjectTwo; import java.util.Scanner; public class ProjectTwo { // ---------------------------------------- // Main method, calls location (loc) method, which

我们试图使用数组存储位置,然后在第一个类的if-else语句中调用它们。我们希望能够调用网格位置,这样就不必在if-else语句中键入房间的描述

package ProjectTwo;   
import java.util.Scanner;
public class ProjectTwo {
    // ----------------------------------------
    // Main method, calls location (loc) method, which controls navigation
    // ----------------------------------------
    public static void main(String[] args){
       loc();
    }
    // This method allows the user to view a list of actions that are used throughout the game
    public static void help() {
        System.out.println("Enter the letter 'n' to move north, the letter 's' to move south, or type the word 'quit' to end the game. Also, you can enter the letter 'm' to see an image of the map.");
    }
    // -------------------------------------
    // Loc method
    // Prints on-load message (intro)
    // Defines global variables
    // -------------------------------------
    public static void loc() {
        location.locMove();

        int location = 0;
        System.out.println("The Search" + "\n" + "\n" + "You have awoken in a laboratory. There is a door to your north and a door to your south." + "/n+" +  "Enter 'n' and 's' to navigate, or type 'quit' to end the game. Also, enter the letter 'h'.");

        String userInput = new String();
        boolean stillPlaying = true;
    // ------------------------------------ 
    // Moves player while user is still playing
    // Tells user his/her location  
    // ------------------------------------
    while (stillPlaying) {
        Scanner scan = new Scanner(System.in);
        userInput = scan.nextLine();
        if (location == 0){
            if (userInput.equals("n")) {
                System.out.println("You entered the dungeon.");
                location = 1; // Moves user from location 0 to 1

            }
                else if (userInput.equals("s")) {
                System.out.println("You cannot move south.");
                location = 0; // Keeps user at location 0
                } else if (userInput.equals("quit")){
                    System.out.println("Thanks for playing");
                    stillPlaying = false;
        }
                else if (userInput.equals("h")) {
                    help(); // calls the help method
                }
                else if (userInput.equals("m")) {
                    map(); // calls the map method
                }
            else {
                System.out.println("Command not understood.");  

            } 

        }   else if (location == 1) {
            if (userInput.equals("n")) {
                System.out.println("You have escaped out the back door of the dungeon.");
                location = 2; // Moves user from location 1 to location 2
            }
            else if(userInput.equals("s")) {
                System.out.println("You're back in the laboratory.");
                location = 0; // Moves user from location 1 to location 0
            } else if (userInput.equals("quit")){
                System.out.println("Thanks for playing");
                stillPlaying = false;
            }
            else if (userInput.equals("h")) {
                help(); // calls the help method
            }
            else if (userInput.equals("m")) {
                map(); // calls the map method
            }
            else {
                System.out.println("Command not understood");

            }
        }

        else if (location == 2) {
            if (userInput.equals("n")) {
                System.out.println("You cannot go that way...yet!");
                location = 2; // Lets the user know that they cannot go that way

            }
            else if (userInput.equals("s")) {
                System.out.println("You're back in the dungeon");
                location = 1; // Mover from location 2 to location 1
                }
        else if (userInput.equals("quit")){
                    System.out.println("Thanks for playing");
                    stillPlaying = false;
                    }
        else if (userInput.equals("h")) {
            help(); // calls help method
        }
        else if (userInput.equals("m")) {
            map(); // calls map method
        }
        else {
                System.out.println("Command not understood.");

                }
        }


    }
}
}

//This is our main class
-------------------------------------------------------------------------
//这是我们的2d数组类

package ProjectTwo;
public class location {

        public int location;
        public String name;
        public static String message;

        public location(String name, int location, String message){
            this.name = name;
            this.location = location;
            this.message = message;

            System.out.println(message);
        }
        public static void locMove() {
        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.");
        }
        while (grid.equals(0)) {
            System.out.println(message.toString());
        }
}
}

有很多方法可以做到你所说的,但是看看你的代码,认为这真的很适合你的代码,没有太多变化,只是我的观点:

“尝试使用,Location notLocation命名类,但不要命名nesesario”

方案二

公共类位置{

在您的班级地点:

//Your other code

public static location [][] locMove() {  // <--- change void for location [][]
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.");
        }
    while (grid.equals(0)) {
        System.out.println(message.toString());
    }

 return grid;
 }

 //Your other code
//您的其他代码

publicstaticlocation[]locMove(){/我完全同意关于代码样式和格式的评论。无论如何,下面是如何重构代码的建议

避免编写大量if-else构造的最简单方法可能是使用开关。您的loc()代码可能是这样的(我还会将重复的h/m/quit命令移动到一个位置):

解决方案1:

...

while (stillPlaying) {
    Scanner scan = new Scanner(System.in);
    userInput = scan.nextLine();

    switch (userInput) {
        case "quit":
            System.out.println("Thanks for playing");
            stillPlaying = false;
            break;

        case "h":
            help(); // calls the help method
            break;

        case "m":
            map(); // calls the map method
            break;

        case "n":
        case "s":
            switch (location) {
                case 0:
                    switch (userInput) {
                        case "n":
                            System.out.println("You entered the dungeon.");
                            location = 1; // Moves user from location 0 to 1
                            break;

                        case "s":
                            System.out.println("You cannot move south.");
                            location = 0; // Keeps user at location 0
                            break;
                    }
                    break;

                case 1:
                    switch (userInput) {
                        case "n":
                            System.out.println("You have escaped out the back door of the dungeon.");
                            location = 2; // Moves user from location 1 to location 2
                            break;

                        case "s":
                            System.out.println("You're back in the laboratory.");
                            location = 0; // Moves user from location 1 to location 0
                            break;
                    }
                    break;

                case 2:
                    switch (userInput) {
                        case "n":
                            System.out.println("You cannot go that way...yet!");
                            location = 2; // Lets the user know that they cannot go that way
                            break;

                        case "s":
                            System.out.println("You're back in the dungeon");
                            location = 1; // Mover from location 2 to location 1
                            break;
                    }
                    break;

                default:
                    System.out.println("no such location");
            }
            break;

        default:
            System.out.println("Command not understood.");

    }
}

...
但是,更好的方法是尝试将位置命令操作内容封装到不同的类中。它可能看起来是这样的:

解决方案2:

public static interface Action {
    // return the next location or an error
    int action();
}

public static class LocationsMap {
    public Map<Integer, Map<String, Action>> locations = new HashMap<>();

    public void registerAction(int location, String userInput, Action action) {
        Map<String, Action> actionsMap = locations.get(location);
        if (actionsMap == null) {
            actionsMap = new HashMap<>();
            locations.put(location, actionsMap);
        }

        actionsMap.put(userInput, action);
    }

    public int executeAction(int location, String userInput) {
        Map<String, Action> currentLocation = locations.get(location);
        if (currentLocation == null) {
            return -1;
        }

        Action currentAction = currentLocation.get(userInput);
        if (currentAction == null) {
            return -2;
        }

        return currentAction.action(); // move to next location
    }
}
然后loc()函数中的代码如下所示:

...

public static LocationsMap locationsMap = new LocationsMap();
static {
    // location 0
    locationsMap.registerAction(0, "n", new Action() {
        @Override
        public int action() {
            System.out.println("You entered the dungeon.");
            return  1; // Moves user from location 0 to 1
        }
    });
    locationsMap.registerAction(0, "s", new Action() {
        @Override
        public int action() {
            System.out.println("You cannot move south.");
            return  0; // Keeps user at location 0
        }
    });

    // location 1
    locationsMap.registerAction(1, "n", new Action() {
        @Override
        public int action() {
            System.out.println("You have escaped out the back door of the dungeon.");
            return 2; // Moves user from location 1 to location 2
        }
    });
    locationsMap.registerAction(1, "s", new Action() {
        @Override
        public int action() {
            System.out.println("You're back in the laboratory.");
            return 0; // Moves user from location 1 to location 0
        }
    });

    // location 2
    locationsMap.registerAction(2, "n", new Action() {
        @Override
        public int action() {
            System.out.println("You cannot go that way...yet!");
            return 2; // Lets the user know that they cannot go that way
        }
    });
    locationsMap.registerAction(2, "s", new Action() {
        @Override
        public int action() {
            System.out.println("You're back in the dungeon");
            return 1; // Mover from location 2 to location 1
        }
    });
}

...
...

while (stillPlaying) {
    Scanner scan = new Scanner(System.in);
    userInput = scan.nextLine();

    switch (userInput) {
        case "quit":
            System.out.println("Thanks for playing");
            stillPlaying = false;
            break;

        case "h":
            help(); // calls the help method
            break;

        case "m":
            map(); // calls the map method
            break;

        default:
            int actionResult = locationsMap.executeAction(location, userInput);

            if (actionResult == -1) {
                System.out.println("no such location");
                break;
            }

            if (actionResult == -2) {
                System.out.println("Command not understood.");
                break;
            }

            location = actionResult; // move to next location

    }
}

...
这不是最好的解决方案,但它更好,更具可读性。 我还将为位置定义枚举,而不是使用int


您也可以考虑将此位置输入操作信息存储在某个文件中,对其进行解析,然后在应用程序/游戏中使用,但实现起来会更复杂。

为什么在
ProjectTwo
类中没有location类的实例?此外,您的代码格式太差,令人不安。实例化location class在ProjectTwo类中,然后您可以使用location对象的引用变量获取数组访问权限。我建议在location类中使用getter和setter,这样您就不会直接修改数组数据。例如,将您的import语句移出您的类体…它出现了(如果我错了,请纠正我)你要求别人修改你的作业,而不是粘贴你所有的作业(格式不是很好)编写代码并询问填鸭式解决方案,为什么不试着将问题分解成一小块,然后问一个更抽象的问题,谁的解决方案以后可能会使其他人受益?实际上,我们所做的是询问如何从不同的类调用数组,以及我们是否需要使用扩展器调用另一个类。谢谢,我非常感谢很多。我会做一些关于枚举的研究来更好地理解它们。谢谢你的输入!
...

while (stillPlaying) {
    Scanner scan = new Scanner(System.in);
    userInput = scan.nextLine();

    switch (userInput) {
        case "quit":
            System.out.println("Thanks for playing");
            stillPlaying = false;
            break;

        case "h":
            help(); // calls the help method
            break;

        case "m":
            map(); // calls the map method
            break;

        default:
            int actionResult = locationsMap.executeAction(location, userInput);

            if (actionResult == -1) {
                System.out.println("no such location");
                break;
            }

            if (actionResult == -2) {
                System.out.println("Command not understood.");
                break;
            }

            location = actionResult; // move to next location

    }
}

...