public class PaintRound{ /** * @author Dream_e. * @version v1.0 */ private int r;//圆的半径 PaintRound(int r){ this.r = r; } public void paint(){ int y = 2*r;//y的最大值为半径的2倍 int x = 0; int c = 0; int z = 2;//坐标轴递减量. for(int j = y; j >= 0; j-=z){ x = getX(r,y); System.out.print(getSpace(x)+"*"); c = (r-x)*2; System.out.println(getSpace(c)+"*"); y-=z; } } //计算X坐标. private int getX(int r, int y){ double temp = Math.sqrt(r*r-(r-y)*(r-y));//根据圆内的正角三角形计算出圆上某点到圆心的X轴距离 int x = (int)Math.round(r-temp); return x; } //计算要打印的空格数量. private String getSpace(int c){ String space = " "; for(int i = 0; i < c; i++){ space += " "; } return space; } public static void main(String[] args){ new PaintRound(10).paint(); } }
疯狂java 讲义第三章练习题---画圆,布布扣,bubuko.com
原文地址:http://blog.csdn.net/dream_e/article/details/25802499