gpt4 book ai didi

java - 为什么我的金字塔不能正确堆叠?

转载 作者:行者123 更新时间:2023-12-01 14:38:06 25 4
gpt4 key购买 nike

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

最佳答案

尝试使用

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

关于java - 为什么我的金字塔不能正确堆叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16288279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com