当我运行以下测试用例时:
@Test(timeout=1000) public void shape_displayChar2_rot(){
String layout =
"b....\n"+
".....\n"+
"....b\n"+
"";
String expect =
"SHAPE z\n"+
"height: 5; width: 3; rotation: CW90\n"+
"..z\n"+
"...\n"+
"...\n"+
"...\n"+
"z..\n"+
"";
char newDC = 'z';
char dc = getDisplayChar(layout);
Shape shape = FitIt.makeShape(layout,dc);
shape.setDisplayChar(newDC);
shape.rotateCW();
assertEquals(newDC, shape.getDisplayChar());
String actual = shape.toString();
assertEquals(expect,actual);
}
我遇到以下失败:
Expected:
SHAPE z
height: 5; width: 3; rotation: CW90
..z
...
...
...
z..
我的实际代码结果:
Actual:
SHAPE z
height: 5; width: 3; rotation: CW90
b....
.....
....b
..z
...
...
...
z..
上述测试用例中的String layout
被rotateCW()
旋转90度ClockWise并且还会更改其字符。我的问题是为什么我得到了:
b.... <<<<----- WHY AM I GETTING THIS ?
..... <<<<----- WHY AM I GETTING THIS ?
....b <<<<----- WHY AM I GETTING THIS ?
..z
...
...
...
z..
而不只是:
..z
...
...
...
z..
代码:
import java.util。*;
public class CreateShape implements Shape {
private String layout;
private int height;
private int width;
private char dc;
private Rotation initialPos;
private char[][] shape;
//private char[][] rotatedShape = new char[height][width];// = new char[shape.length][shape[0].length];
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;
//nextPos = Rotation.CW0;
}
public Rotation getRotation() {
return initialPos;
}
public int getHeight() {
return this.height;
}
public int getWidth() {
return this.width;
}
public char getDisplayChar() {
return dc;
}
public void setDisplayChar(char c) {
this.dc = c;
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[0].length; j++) {
//if(shape[i][j] == '.')
//this.newLayout += shape[i][j];
if (shape[i][j] != '.') {
shape[i][j] = c;
}
}
//this.newLayout+="\n";
}
}
public void rotateCW() {
initialPos = initialPos.next();
int w = shape.length;
int h = shape[0].length;
char[][] 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;
}
public boolean isFilledAt(int row, int col) {
if (row < 0 || row >= height || col < 0 || col >= width) {
throw new FitItException("Oops! Out of bounds!");
} else {
for (int r = 0; r <= height; r++) {
for (int c = 0; c <= width; c++) {
if (shape[row][col] == dc) {
return true;
}
}
}
}
return false;
}
public String toString() {
return "SHAPE " + this.dc + "\n" +
"height: " + this.height + ";" + " width: " + this.width + "; " + getRotation().toString() + "\n" +
this.layout;
}
}
class FitIt
public class FitIt {
public static Shape makeShape(String layout, char displayChar) {
Shape result;
int height = 0;
int width = 0;
Scanner data = new Scanner(layout);
while (data.hasNextLine()) {
String line = data.nextLine();
width = line.length();
height++;
}
char[][] charLayout = new char[height][width];
Scanner data2 = new Scanner(layout);
for (int i = 0; i < height; i++) {
String line = data2.nextLine();
for (int j = 0; j < width; j++) {
if (line.charAt(j) != '.') {
charLayout[i][j] = displayChar;
}
if (line.charAt(j) == '.') {
charLayout[i][j] = line.charAt(j);
}
}
}
data2.close();
result = new CreateShape(height, width, displayChar, charLayout, layout);
return result;
}
答案 0 :(得分:3)
在rotateCW()
方法中,您忘记清除layout
。您可以在for循环之前添加layout = ""
并尝试。
答案 1 :(得分:3)
旋转文本时,您将旋转的线条附加到当前布局。
你不是从一个新的String
开始的。因此,您的原始布局将保留在结果中。