菜鸟。这应该创建一个金字塔,但正如你所看到的,我无法正确设置我的x轴。我不知道该怎么做。有帮助吗?谢谢 - 基思。
import acm.graphics.*;
import acm.program.*;
public class Pyramid extends GraphicsProgram {
public void run()
{
double xCoord = 50;
double yCoord = 200;
double base = BRICKS_IN_BASE;
int cnt = 0;
while ( cnt < base )
//for (int n = 0; n < base; n++)
{
for (int i = 0; i < base; i++)
{
add(new GRect(xCoord, yCoord, BRICK_WIDTH, BRICK_HEIGHT));
xCoord += BRICK_WIDTH;
}
base--;
yCoord -= BRICK_HEIGHT;
xCoord = ???????????????
}
}
private static final double BRICK_WIDTH = 10;
private static final double BRICK_HEIGHT = 12;
private static final double BRICKS_IN_BASE = 14;
private static final double X_BASE = 25;
}
答案 0 :(得分:1)
尝试
xCoord -= (base * BRICK_WIDTH) + (BRICK_WIDTH/2);
或者,这个
while ( cnt < base )
//for (int n = 0; n < base; n++)
{
int initX = xCoord;
for (int i = 0; i < base; i++)
{
add(new GRect(xCoord, yCoord, BRICK_WIDTH, BRICK_HEIGHT));
xCoord += BRICK_WIDTH;
}
base--;
yCoord -= BRICK_HEIGHT;
xCoord = initX + BRICK_WIDTH/2;
}