2011-06-24 8 views
0

私は、次のJavaファイルのセグメントしている操作する:のJava switch文が列挙型の場合に

Integer x; Integer y; Face facing; 

enum Rotate { Clockwise, Anticlockwise }; 

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

とは、被写体の顔を変更する機能を実装する方法を考え出すトラブルを抱えています(つまり、方向をそのオブジェクトが向いている)。

次のように私は(テキストの下に内部で上記ブレース)switch文を使用し始めている

private void rotateIt(Rotate rotateIt) { 
    { 

次のように機能が始まる:

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

私はClockwise列挙を使用したいがEastからSouthなど、Anticlockwiseから逆方向IYGWIMを実行することができます。

+0

この宿題はありますか? – Bueller

+0

用語はCounterClockwise – Woot4Moo

答えて

4
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; 
} 

私は遡って大きなグレードのパーセントを取得する必要があります。あなたは罰金を開始している

+0

あなたの三元演算子であり、あなたはきれいなコードスタイルです。 – Grammin

+0

@Grammin、特に同じコードを何度も何度も繰り返してくっついているとき、敏感な考えについて何か言いたいことがあります。 'rotateIt == Rotate.Clockwise'を' bool'に保存して使うことができますが、これはあまり改善されていません。 – Blindy

+0

私はそれがさらに単純化できる方法があると思います。 – maclunian

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

のように...。ここでは、列挙型の操作について何をすべきかのより完全なバージョンは次のとおりです。もちろん

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; 
} 

} 

、このコードを最適化することができますが、それはあなたのenumsswitch内の文を処理する方法のアイデアを提供します。

1

もう一つの選択肢は、enumを使用して作業を行うことです。

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); 
+0

「oridinal」に依存しているIMOは、ほとんど常に悪い選択です。私はむしろ顔に数値的な重みを与えてもらいたい。また、 'rotate'の実装にはまだ' abstract'キーワードがあります。しかし、間違いなくここに掲示されたものより良い解決策、+1。 –

+0

@Sanjay、あなたのコメントありがとう、私は私の答えを編集しました。 –

2

私は顔の向きの関数として回転を実装します:

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 

このコードは、めったに使われない「序を使用しています'Enumのプロパティ。従って、値は論理的な順序であることが必要である。 (東、北、西、南)