Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从switch语句返回所有可能的结果_Java - Fatal编程技术网

Java 从switch语句返回所有可能的结果

Java 从switch语句返回所有可能的结果,java,Java,我想根据播放器输入的步长返回每个相邻方向。输入不能是char,必须是int(不能只是说向南走,必须是从地图开始的几步,我会把它放在底部)。使用nextArea方法感觉极度失落。 请不要给我写答案,我只是想得到一些指导 这些是GameWorld类中的方法 /** * Returns true if areas s1 and s2 are connected, false otherwise. * Also returns false if either area is an invalid a

我想根据播放器输入的步长返回每个相邻方向。输入不能是char,必须是int(不能只是说向南走,必须是从地图开始的几步,我会把它放在底部)。使用nextArea方法感觉极度失落。 请不要给我写答案,我只是想得到一些指导

这些是GameWorld类中的方法

/**
 * Returns true if areas s1 and s2 are connected, false otherwise.
 * Also returns false if either area is an invalid area identifier.
 * @param s1 the first area
 * @param s2 the second area
 * @return true if areas are connected, false otherwise
 */
private boolean areasConnected(int s1, int s2) {
    if (Math.min(s1, s2) >= 0 && Math.max(s1, s2) < numAreas) { //valid areas...
        //...but are they connected? 
        return east[s1] == s2 || north[s1] == s2 || west[s1] == s2 || south[s1] == s2;
    }
    //Either s1 or s2 is not a valid area identifier
    return false;
}


/**
 * Determine ID number of an adjacent area given its direction from the
 * current area.
 * @param direction the direction to look (n for north, e for east, s for    south, w for west)
 * @return number of the area in that direction
 * @throws IllegalArgumentException if direction is null
 */
    public int nextArea(char direction) {
    int nextIs;    // area number of area in indicated direction

    //Valid values
    final char N = 'n', E = 'e', S = 's', W = 'w';

    trace("nextArea() starts...");

    // examine adjacent areas
    switch (direction) {
        case N: trace("determining number of area to the north");
                nextIs = north[currentArea];
                break;
        case E: trace("determining number of area to the east");
                nextIs = east[currentArea];
                break;
        case S: trace("determining number of area to the south");
                nextIs = south[currentArea];
                break;
        case W: trace("determining number of area to the west");
                nextIs = west[currentArea];
                break;
        default: throw new IllegalArgumentException("Direction must be one of " + N + ", " + E + ", " + S + " or " + W);
    }

    trace("...nextArea() ends with value for '" + direction + "' of " + nextIs);

    return nextIs;
}
gameOne.nextArea()

}

这是GameWorld类中具有地图的方法

//This map is _deliberately_ confusing, although it actually a regular layout
private int[] east =  {1,2,0,4,5,3,7,8,6}; // areas to east of current location (index)
private int[] west =  {2,0,1,5,3,4,8,6,7}; // areas to west of current location (index)
private int[] north = {6,7,8,0,1,2,3,4,5}; // areas to north of current location (index)
private int[] south = {3,4,5,6,7,8,0,1,2}; // areas to south of current location (index)
private int numAreas = south.length;     // number of areas in the "world"

如果我正确地解释了你的问题,那么你是在问如何将索引列表(表示位置)更改为达到这些位置所必须采取的步骤

您要求的是提示而不是代码,因此我将给出一些一般性的提示,如果您需要更多详细信息,可以让我知道

我认为,如果将方向转换为
enum
,您会发现您的解决方案更具可读性和清晰性。这样,所有与任何方向关联的代码都在该枚举内,您可以避免在其他地方使用
switch
语句

例如:

public enum Direction {
    N(north), S(south), E(east), W(west);

    private final int[] locations;
    Direction(int[] locations) {
        this.locations = locations;
    }

    public int next(int current) {
        return locations[current];
    }
}
您可以使用
direction.valueOf(dir.toUpper())
从名称中获取方向,然后使用
dir.next(current)
获取方向中的下一个位置

您的评论指出,您需要朝相反的方向走:采取一个立场,并返回指向该立场的立场。这需要迭代:您需要在数组中搜索该位置。同样,这可能是枚举中的一个方法。下面是一个例子:

public int previous(int current) {
    for (int i = 0; i < locations.length; i++) {
        if (locations[i] == current)
            return i;
    }
    return -1;
}

一旦你有了地图,你就可以很容易地得到所有以前的位置(
map.keySet()
)以及从给定位置(
map.get(location)
)遵循的方向。

如果我正确地解释了你的问题,那么你就是在问如何更改索引列表(表示位置)进入必须采取的步骤,以达到这些立场

您要求的是提示而不是代码,因此我将给出一些一般性的提示,如果您需要更多详细信息,可以让我知道

我认为,如果将方向转换为
enum
,您会发现您的解决方案更具可读性和清晰性。这样,所有与任何方向关联的代码都在该枚举内,您可以避免在其他地方使用
switch
语句

例如:

public enum Direction {
    N(north), S(south), E(east), W(west);

    private final int[] locations;
    Direction(int[] locations) {
        this.locations = locations;
    }

    public int next(int current) {
        return locations[current];
    }
}
您可以使用
direction.valueOf(dir.toUpper())
从名称中获取方向,然后使用
dir.next(current)
获取方向中的下一个位置

您的评论指出,您需要朝相反的方向走:采取一个立场,并返回指向该立场的立场。这需要迭代:您需要在数组中搜索该位置。同样,这可能是枚举中的一个方法。下面是一个例子:

public int previous(int current) {
    for (int i = 0; i < locations.length; i++) {
        if (locations[i] == current)
            return i;
    }
    return -1;
}

一旦你有了地图,你就可以很容易地得到所有以前的位置(
map.keySet()
)以及从给定位置(
map.get(location)
)要遵循的方向。

你能更详细地解释一下这个问题吗?你的意思是输入是数组中的一组“位置”索引,并且你需要返回执行每个步骤所必须采取的方向吗?输入是人员想要进入的位置。返回应该根据位置数组(或地图)说明该区域周围的区域编号。输入是该人员想要进入的位置。返回应该根据位置数组(或地图)说明围绕该区域的区域号。您能更详细地解释一下问题吗?你的意思是输入是数组中的一组“位置”索引,并且你需要返回执行每个步骤所必须采取的方向吗?输入是人员想要进入的位置。返回应该根据位置数组(或地图)说明该区域周围的区域编号。输入是该人员想要进入的位置。返回值应根据位置数组(或地图)说明该区域周围的区域编号。
Map<Integer,Direction> allPrevious(int location) {
    Map<Integer,Direction> map = new HashMap<>();
    for (Direction dir: Direction.values()) {
        int prev = dir.previous(location);
        if (prev >= 0)
            map.put(prev, dir);
    }
}