Java Switch 语句操作 Enum 大小写

发布于 2024-11-17 13:08:48 字数 573 浏览 2 评论 0原文

我有一个java文件的以下片段:

Integer x; Integer y; Face facing;

enum Rotate { Clockwise, Anticlockwise };

enum Face { East, North, West, South };

并且无法弄清楚如何实现一个函数来改变对象的面(即对象面向的方向)。

该函数开始如下

private void rotateIt(Rotate rotateIt) {
    {

我已经开始使用 switch 语句如下(下面的文本在上面的大括号内):

switch (facing)
case North  : ...?;
case West   : ...?;
case East   : ...?;
case South  : ...?;

我想使用 Clockwise 枚举将其从 East 等并逆时针 进行相反的IYGWIM。

I have the following segment of a java file:

Integer x; Integer y; Face facing;

enum Rotate { Clockwise, Anticlockwise };

enum Face { East, North, West, South };

and am having trouble figuring out how to implement a function to change the Face of an object (i.e. the direction that the object is facing).

The function begins as follows

private void rotateIt(Rotate rotateIt) {
    {

I have begun using the switch statement as follows ( below text is inside braces above ):

switch (facing)
case North  : ...?;
case West   : ...?;
case East   : ...?;
case South  : ...?;

I'd like to use the Clockwise enumeration to turn it from East to South etc. and Anticlockwise to do the reverse IYGWIM.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

江湖正好 2024-11-24 13:08:48
switch (facing) {
  case North  : facing=rotateIt==Rotate.Clockwise?Face.East:Face.West; break;
  case West   : facing=rotateIt==Rotate.Clockwise?Face.North:Face.South; break;
  case East   : facing=rotateIt==Rotate.Clockwise?Face.South:Face.North; break;
  case South  : facing=rotateIt==Rotate.Clockwise?Face.West:Face.East; break;
}

我应该可以追溯你的成绩的很大一部分!

switch (facing) {
  case North  : facing=rotateIt==Rotate.Clockwise?Face.East:Face.West; break;
  case West   : facing=rotateIt==Rotate.Clockwise?Face.North:Face.South; break;
  case East   : facing=rotateIt==Rotate.Clockwise?Face.South:Face.North; break;
  case South  : facing=rotateIt==Rotate.Clockwise?Face.West:Face.East; break;
}

I should get a large percent of your grade retroactively!

谁的年少不轻狂 2024-11-24 13:08:48

我会将旋转实现为面部方向的函数:

 enum RotationDirection { Clockwise, CounterClockwise };
 enum Face {
    East, North, West, South ;

    Face rotate(RotationDirection direction ) {
        int tick  = (direction == RotationDirection.Clockwise)?-1:1;
        int index = this.ordinal()+tick ;
        int length = Face.values().length-1;
        if (index <0) index = length;
        if (index >length) index = 0;
        return Face.values()[index];
    }

然后您可以执行以下操作:

 Face face = Face.North;
 face.rotate(RotationDirection.Clockwise); // East
 face.rotate(RotationDirection.CounterClockwise); //West 

此代码利用了很少使用的枚举的“序数”属性。因此,它要求这些值按逻辑顺序排列,例如(东、北、西、南)

I would implement rotate as a function of the Face orientation:

 enum RotationDirection { Clockwise, CounterClockwise };
 enum Face {
    East, North, West, South ;

    Face rotate(RotationDirection direction ) {
        int tick  = (direction == RotationDirection.Clockwise)?-1:1;
        int index = this.ordinal()+tick ;
        int length = Face.values().length-1;
        if (index <0) index = length;
        if (index >length) index = 0;
        return Face.values()[index];
    }

Then you can do things like:

 Face face = Face.North;
 face.rotate(RotationDirection.Clockwise); // East
 face.rotate(RotationDirection.CounterClockwise); //West 

This code makes use of the seldom used 'ordinal' property of Enums. It therefore requires that the values are in a logical turning order e.g. (east, north, west, south)

明月松间行 2024-11-24 13:08:48

另一种选择是使用枚举来完成这项工作。

enum Face { 
    North, East, South, West; // must be clockwise order.
}

enum Rotate { 
    private static final Face[] FACES = Face.values();
    Clockwise {
        public Face rotate(Face face) {
            return FACES[(ordinal()+1)%FACES.length];
        }
    }, 
    Anticlockwise {
        public Face rotate(Face face) {
            return FACES[(ordinal()+FACES.length-1)%FACES.length];
        }
    }
    public abstract Face rotate(Face face);
};

facing = rotateIt.rotate(facing);

Another option is to use the enum to do the work.

enum Face { 
    North, East, South, West; // must be clockwise order.
}

enum Rotate { 
    private static final Face[] FACES = Face.values();
    Clockwise {
        public Face rotate(Face face) {
            return FACES[(ordinal()+1)%FACES.length];
        }
    }, 
    Anticlockwise {
        public Face rotate(Face face) {
            return FACES[(ordinal()+FACES.length-1)%FACES.length];
        }
    }
    public abstract Face rotate(Face face);
};

facing = rotateIt.rotate(facing);
别理我 2024-11-24 13:08:48
case North:
{
  if(rotateIt == Rotate.Clockwise)
    facing = Face.EAST
  else
    facing = Face.WEST
  break;
}

等等...

case North:
{
  if(rotateIt == Rotate.Clockwise)
    facing = Face.EAST
  else
    facing = Face.WEST
  break;
}

and so on...

笑着哭最痛 2024-11-24 13:08:48

你开始得很好。以下是关于枚举操作必须执行的操作的更完整版本:

public void RotateIt(Rotate toRotate, Face facing) {

switch (facing) {
    case North:
        // Include code to rotate from north
        break;
    case West:
        // Include code to rotate from west
        break;
    case East:
        // Include code to rotate from east
        break;
    default: // South
        // Include code to rotate from south
        break;
}

}

当然,可以优化此代码,但它让您了解如何在 switch 中处理 enum代码> 语句。

You are starting fine. Here is a more complete version of what you have to do regarding enum manipulation:

public void RotateIt(Rotate toRotate, Face facing) {

switch (facing) {
    case North:
        // Include code to rotate from north
        break;
    case West:
        // Include code to rotate from west
        break;
    case East:
        // Include code to rotate from east
        break;
    default: // South
        // Include code to rotate from south
        break;
}

}

Of course, this code could be optimized, but it gives you an idea of how to handle enums in switch statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文