该方法每次都给出相同的结果

时间:2015-04-26 17:53:28

标签: java

我的方法rotateCW()应该在每次调用时将一个Shape顺时针旋转90度。如果执行以下操作:

someObject.rotateCW();
someObject.rotateCW();
someObject.rotateCW();

然后,从270开始,形状将从时钟方向旋转到0度。

但我的rotateCW()代码只会​​旋转到90度,无论我调用它多少次。例如:

x . a 
. . b
x . c

在调用rotateCW()两次(180)后,表单应该变为:

someObject.rotateCW();
someObject.rotateCW();

c . x
b . . 
x . a

但我得到的只是:

x . x
. . .
c b a 

无论我拨打rotateCW多少次,我的代码只会旋转一次!

import java.util.Scanner;
public class Tester {

    public static void main(String[] args)
    {

        //FitIt temp = new FitIt();
        //CreateShape result = (CreateShape) temp.makeShape(". . .\n"+"n . n", 'a');

    CreateShape temp = new CreateShape(6,6, 'a', new char[][]{{'x','.','a'}, 
                                                                  {'.','.','b'}, 
                                                                  {'x','.','c'}}, "x . a\n"
                                                                                + ". . b\n"
                                                                                + "x . c");   
        temp.rotateCW();
        temp.rotateCW();
        temp.rotateCW();
        System.out.println(temp);
}
import java.util.*;

public class CreateShape implements Shape {

//  private String newLayout  = "";
    private String layout;
    private int height;
    private int width;
    private char dc;
    private Rotation initialPos;
    private char[][] shape; 
    private char[][] swapped;


    public CreateShape(int height, int width, char dc, char[][] charLayout, String layout)
    {
        this.height = height;
        this.width = width;
        this.dc = dc;
        this.shape = charLayout;
        this.layout = layout;
        initialPos = Rotation.CW0;
    }

    public void rotateCW() 
    {
        layout = "";
        initialPos = initialPos.next();
        int w = shape.length;
        int h = shape[0].length;

        swapped = new char[h][w];
        for(int i = 0; i < h; i++)
        {
            for(int j = 0; j < w; j++)
            {
                swapped[i][j] = shape[w-j-1][i];
                layout += swapped[i][j];
            }
            layout += "\n";
        }
        height = swapped.length;
        width = swapped[0].length;
        //newLayout = layout;
    }
public String toString()
    {
        return "SHAPE " + this.dc +"\n" +
                "height: " + this.height+";" + " width: " + this.width+"; " + getRotation().toString() + "\n" +
                 this.layout;
    }

枚举旋转

public enum Rotation {

    CW0, CW90, CW180, CW270;

    // Calling rot.next() will return the next enumeration element
    // representing the next 90 degree clock-wise rotation after rot.
    public Rotation next()
    {
        if(this == CW0)
            return CW90;
        else if(this == CW90)
            return CW180;
        else if(this == CW180)
            return CW270;
        else if(this == CW270)
            return CW0;

        return null;
    }

    //toString representation of rotation
    @Override
    public String toString()
    {
        if(this == CW0)
            return "rotation: CW0";
        else if(this == CW90)
            return "rotation: CW90";
        else if(this == CW180)
            return "rotation: CW180";
        else if(this == CW270)
            return "rotation: CW270";

        return "Something is wrong! Check ROTATION again!";
    }
}

1 个答案:

答案 0 :(得分:1)

执行交换后,您不会更新形状。添加shape = swapped会使旋转持续存在,而不是再次在原始形状上调用旋转:

  public void rotateCW() {
    layout = "";
    initialPos = initialPos.next();
    int w = shape.length;
    int h = shape[0].length;

    swapped = new char[h][w];
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        swapped[i][j] = shape[w - j - 1][i];
        layout += swapped[i][j];
      }
      layout += "\n";
    }
    height = swapped.length;
    width = swapped[0].length;
    shape = swapped;
  }
  public static void main(String[] args) {
    CreateShape temp = new CreateShape(6, 6, 'a', new char[][]{{'x', '.', 'a'},
        {'.', '.', 'b'},
        {'x', '.', 'c'}}, "x . a\n"
        + ". . b\n"
        + "x . c");
    temp.rotateCW();
    System.out.println(temp);
    temp.rotateCW();
    System.out.println(temp);
    temp.rotateCW();
    System.out.println(temp);
  }
SHAPE a
height: 3; width: 3; rotation: CW90
x.x
...
cba

SHAPE a
height: 3; width: 3; rotation: CW180
c.x
b..
a.x

SHAPE a
height: 3; width: 3; rotation: CW270
abc
...
x.x