切换两个INT变量的情况

时间:2013-04-13 18:10:57

标签: java switch-statement

请考虑以下代码:

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,但如何将switch2变量一起使用?

我想到了this之类的内容 - @Frits van Campen的答案,但我需要使用><运算符......

谢谢

6 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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..
}

答案 2 :(得分: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 cascades需要一些运行时并且难以管理。
  • 不创建临时字符串。在严格的游戏循环中,这可能很重要。
  • 不通过列表或数组进行线性搜索。

答案 3 :(得分: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;
}

答案 4 :(得分: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];

在课程加载时初始化directions static 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
   ...
}

使用从三元组,shift和或运算符计算的整数对数组建立索引比在字符串切换中使用的字符串连接消耗更少的CPU,并且可以从Java 1.0中很好地工作。

答案 5 :(得分: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的建议。