0

小白。这应该创建一个金字塔,但如您所见,我无法正确设置我的 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;
}
4

1 回答 1

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;
    }
于 2013-04-29T21:26:50.290 回答