6

考虑以下代码:

if (xPoint > 0 && yPoint > 0) {
    m_navigations = Directions.SouthEast;
}
else if (xPoint > 0 && yPoint < 0) {
    m_navigations = Directions.NorthEast;
}
else if (xPoint < 0 && yPoint > 0) {
    m_navigations = Directions.SouthWest;
}
else if (xPoint < 0 && yPoint < 0) {
    m_navigations = Directions.NorthWest;
}
else if (xPoint == 0 && yPoint < 0) {
    m_navigations = Directions.North;
}
else if (xPoint == 0 && yPoint > 0) {
    m_navigations = Directions.South;
}
else if (xPoint > 0 && yPoint == 0) {
    m_navigations = Directions.East;
}
else if (xPoint < 0 && yPoint == 0) {
    m_navigations = Directions.West;
}

这很难看,我想使用 switch case,但是如何使用switch变量2

我想过这样的事情- @Frits van Campen 的答案,但我需要使用><运营商......

谢谢

4

6 回答 6

6

你可以用枚举做任何事情。我为前两个值创建了示例,您可以继续其余的。

public enum Direction
{
    SouthEast(1,1),
    NorthEast(1,-1);

    int _xPoint, _yPoint;

    Direction(int xPoint, int yPoint)
    {
        _xPoint = xPoint;
        _yPoint = yPoint;
    }

    public static Direction getDirectionByPoints(int xPoint, int yPoint)
    {
        for (Direction direction : Direction.values())
        {
            if(   Integer.signum(xPoint) == direction._xPoint 
               && Integer.signum(yPoint) == direction._yPoint )
            {
                return direction;
            }
        }
        throw new IllegalStateException("No suitable Direction found");
    }
}

所以你可以打电话:

m_navigations = Direction.getDirectionByPoints(xPoint,yPoint);
于 2013-04-13T18:54:32.343 回答
2

使用 signum 在这样的方向上获得 -1、0 或 1:

String direction = Integer.signum(xPoint)+","+Integer.signum(yPoint);
switch(direction){
  case "1,1": 
    m_navigations = Directions.SouthEast;
    break;
  case "-1,0"
    m_navigations = Directions.West;
    break;

etc..
}
于 2013-04-13T18:28:52.860 回答
2

最简单和最简单的解决方案是使用多维数组。

public class CalculateDirections {
    private final static Directions DIRECTION_MAP[][] = {
        {Directions.NorthWest, Directions.North, Directions.NorthEast},
        {Directions.West, null, Directions.East},
        {Directions.SouthWest, Directions.South, Directions.SouthEast},
    };

    public static void main(String[] args) {
        int x = Integer.valueOf(args[0]);
        int y = Integer.valueOf(args[1]);

        int signumX = Integer.signum(x);
        int signumY = Integer.signum(y);
        Directions direction = DIRECTION_MAP[signumY + 1][signumX + 1];

        System.out.println(direction);
    }
}

enum Directions {
    SouthEast, NorthEast, SouthWest, NorthWest, North, South, East, West
}

有几个优点:

  • 没有 if/else 级联,它们需要一些运行时间并且难以管理。
  • 不创建临时字符串。在紧凑的游戏循环中,这可能很重要。
  • 不能通过列表或数组进行线性搜索。
于 2013-04-13T19:19:08.307 回答
1

与其他答案类似,但没有字符串。只是为了好玩 :-)

public Directions getDirection(int xPoint, int yPoint) {
    int num = 8 * (xPoint == 0 ? 0 : xPoint > 0 ? 1 : 2);
    num += yPoint == 0 ? 0 : yPoint > 0 ? 1 : 2;
    switch (num) {
    case 01:
        return Directions.South;
    case 02:
        return Directions.North;
    case 010:
        return Directions.East;
    case 011:
        return Directions.SouthEast;
    case 012:
        return Directions.NorthEast;
    case 020:
        return Directions.West;
    case 021:
        return Directions.SouthWest;
    case 022:
        return Directions.NorthWest;
    }
    return Directions.None;
}
于 2013-04-13T19:14:39.157 回答
0
boolean xNeg  = xPoint  < 0;
boolean yNeg  = yPoint  < 0;
boolean xZero = xPoint == 0;
boolean yZero = yPoint == 0;

我们有四个位,我们有 2^4 种可能性,一个方向数组可以做剩下的事情......

int index =
   ((xNeg ?1:0)<<3)|
   ((yNeg ?1:0)<<2)|
   ((xZero?1:0)<<1)|
   ((yZero?1:0)<<0);
Directions dir = directions[index];

在类加载时初始化directionsstatic final方向数组。

static final Directions[] directions = {
   Direction.NorthEast, // false, false, false, false ==> x  > 0 && y  > 0
   Direction.East,      // false, false, false, true  ==> x  > 0 && y == 0
   Direction.North,     // false, false, true , false ==> x == 0 && y  > 0
   ...
}

使用从三元、移位和/或运算符计算的整数索引数组比字符串切换中使用的字符串连接消耗的 CPU 更少,并且在 Java 1.0 中运行良好。

于 2013-04-13T18:26:45.303 回答
0

在这一刻 :

    String direction = Integer.signum(xPoint) + "|" + Integer.signum(yPoint);
    switch(direction)
    {
        case "1|1":
            {m_navigations = Directions.SouthEast; break;}
        case "1|-1":
            {m_navigations = Directions.NorthEast; break;}
        case "-1|1":
            {m_navigations = Directions.SouthWest; break;}
        case "-1|-1":
            {m_navigations = Directions.NorthWest; break;}
        case "0|-1":
            {m_navigations = Directions.North; break;}
        case "0|1":
            {m_navigations = Directions.South; break;}
        case "1|0":
            {m_navigations = Directions.East; break;}
        case "-1|0":
            {m_navigations = Directions.West; break;}
        default: break;         
    }

现在我将尝试@danieln 的建议。

于 2013-04-13T19:01:22.097 回答