2013-04-29 48 views
0

Noob。这应该会创建一个金字塔,但正如您所看到的,我无法正确设置我的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; 
} 
+2

你能告诉我们输出不知何故?截图? – hexafraction 2013-04-29 21:09:44

+1

和预期的输出。 – Guvante 2013-04-29 21:10:45

+0

输出,而不是一个有14个块基的金字塔,然后13居中,然后12居中,等到1,而是14块,然后它向上移动一条线并画出一条13块的线,但它坐在上一行的末尾(下面一行),而不是堆叠在顶部并继续到1.我的问题是我无法让金字塔正确堆叠。我怀疑这是xCoord轴的问题。 – 2013-04-29 21:14:22

回答

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; 
    }