您好我正在学习Java并找到一个非常整洁的解决方案, 但我对第二个for循环中的特定代码行感兴趣。 我不知道该问谁,因为我还没上学,因为我在这里问, 但无论如何:
for (int i = 0; i < BRICKS_IN_BASE + (-h); i++)
这是否意味着'重复i
这个次数'被添加'到'h
减去1'?更具体地说是(-h)
是什么?它是预增量吗?为什么它在括号中?
它再次用于声明变量x
。
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class Pyramid extends GraphicsProgram {
/** Width of each brick in pixels */
private static final int BRICK_WIDTH = 30;
/** Width of each brick in pixels */
private static final int BRICK_HEIGHT = 12;
/** Number of bricks in the base of the pyramid */
private static final int BRICKS_IN_BASE = 15;
public void run() {
for (int h = 0; h < BRICKS_IN_BASE; h++)
{
for (int i = 0; i < BRICKS_IN_BASE + (-h); i++)
{
int k = i * BRICK_WIDTH;
int m = h * BRICK_HEIGHT;
int x = ((getWidth() - ((BRICKS_IN_BASE + (-h)) * BRICK_WIDTH)) / 2) + k;
int y = getHeight() - ((BRICK_HEIGHT + 1) + m);
GRect brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);
add(brick);
}
}
}
}
答案 0 :(得分:0)
for (int i = 0; i < BRICKS_IN_BASE + (-h); i++)
确实是(( - h)只是-1 * h)
for (int i = 0; i < (BRICKS_IN_BASE - h); i++)