首先,do-while 循环并不常见(尽管它们有时确实有用)——最好熟悉while
and for
。
由于这似乎是一个家庭作业问题,我要做的是向您展示一些使用多边形绘制同心正方形的代码(这不是正常的方式,您通常只需使用g.drawRect()
:
int width = 200;
int height = 200;
int xMid = width/2;
int yMid = width/2;
while(width > 0 && height > 0) {
// Draw the square
int xLeft = xMid - width/2;
int xRight = xMid + width/2;
int yTop = yMid - height/2;
int yBottom = yMid + height/2;
int[] xPoints = {xLeft, xRight, xRight, xLeft};
int[] yPoints = {yTop, yTop, yBottom, yBottom};
int nPoints = 4;
g.drawPolygon(xPoints, yPoints, nPoints);
// Change the dimensions
width -= 20;
height -= 20;
}
看看你是否可以修改它来绘制三角形。