【发布时间】:2014-02-04 03:45:45
【问题描述】:
这是我的代码的预期输出:
这是我的代码:
import java.awt.*;
public class CafeWall
{
final static int MORTAR = 2;
final static int WIDTH = 650;
final static int HEIGHT = 400;
public static void main( String[] args )
{
StdDraw.setCanvasSize(WIDTH, HEIGHT);
StdDraw.setXscale(0, WIDTH);
StdDraw.setYscale(0, HEIGHT);
// Change from Color.GRAY to change background color.
StdDraw.setPenColor( Color.GRAY );
StdDraw.filledRectangle(WIDTH/2, HEIGHT/2, WIDTH/2, HEIGHT/2);
drawRow( 0, 378, 4, 20 );
drawRow( 50, 300, 5, 30 );
drawGrid( 10, 36, 4, 25, 0 );
drawGrid( 250, 40, 3, 25, 10 );
drawGrid( 425, 2, 5, 20, 10 );
drawGrid( 400, 234, 2, 35, 35 );
}
// Draw a row of squares, the total number of squares is pairs * 2
// (x, y) is the lower left corner of the first box
public static void drawRow( int x, int y, int pairs, int size )
{
StdDraw.setPenColor(Color.BLACK);
StdDraw.filledRectangle(x, y, size, size);
StdDraw.setPenColor(Color.BLUE);
StdDraw.line(x, y, x+size, y+size);
StdDraw.line(x, y+size, x+size, y);
StdDraw.setPenColor(Color.WHITE);
StdDraw.filledRectangle(x+size, y, size, size);
}
// Draw a grid of 2 * pairs rows
public static void drawGrid( int x, int y, int pairs, int size,
int offset )
{
int startingX = x;
for(int line = 1; line <= pairs; line++) {
if(line % 2 ==1) { //if line is odd
x = startingX;
} else { //else line is even and must be shifted by offset specified
x = startingX + offset;
}
for(int i = 1; i <= pairs; i++) {
drawRow(x, y, pairs, size);
x = x + 2*size;
}
y = y + size + MORTAR;
}
}
}
问题:
- 我的行没有正确偏移
- 我的蓝色“x”没有出现。
我已经盯着这个看了好几个小时了。我似乎无法弄清楚我哪里出错了。谁能提供一些指导?
(补充说明) 对于那些想知道的人来说,这是这段代码的实际输出:
【问题讨论】:
-
嗯......你的程序的输出是什么样的?此外,应该向其他所有人注意,中下和右下图案的行高度没有变化——这是一种视觉错觉。如果需要确认,请使用鼠标图标进行测量。
-
我们回答问题。您应该删除您的帖子并将其表述为问题。
-
你发布你的输出怎么样?
-
刚刚发布了在我的系统上运行的 OP 代码的输出。
-
当您整理好代码并正常运行后,请花点时间在CodeReview 上重新提交。