pyramid.java
来自「斯坦福大学的Java课上的作业」· Java 代码 · 共 47 行
JAVA
47 行
/*
* File: Pyramid.java
* Name:
* Section Leader:
* ------------------
* This file is the starter file for the Pyramid problem.
* It includes definitions of the constants that match the
* sample run in the assignment, but you should make sure
* that changing these values causes the generated display
* to change accordingly.
*/
import acm.graphics.*;
import acm.program.*;
public class Pyramid extends GraphicsProgram {
/**
*
*/
private static final long serialVersionUID = 1L;
/** 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 = 14;
public void run() {
int ox = getWidth() / 2;
int x = ox - BRICK_WIDTH * (BRICKS_IN_BASE / 2);
int y = getHeight();
int pace = BRICK_WIDTH / 2;
for(int i = 0; i < BRICKS_IN_BASE; i++){
for(int j = BRICKS_IN_BASE - i ; j > 0; j--){
add(new GRect(x + j*BRICK_WIDTH + i * pace, y - i*BRICK_HEIGHT, BRICK_WIDTH, BRICK_HEIGHT));
// add(new GRect(x + i*BRICK_WIDTH + j * pace, y - j*BRICK_HEIGHT, BRICK_WIDTH, BRICK_HEIGHT));
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?