本帖最后由 王永荣 于 2012-11-7 11:13 编辑
思路:
实现代码如下:- public class PaintCircleDemo{
- public static void main(String[] args){
- Circle c = new Circle(10);
- c.paint();
- }
- }
- class Circle{
- private int radius;
- Circle(int r){
- radius = r;
- }
- public void paint(){
- int x,y,z;
- for(y=0;y<=2*radius;y++){
- x = radius-(int)Math.round(Math.sqrt(Math.pow(radius,2)-Math.pow(y-radius,2)));
- print(space(x));
- print("*");
- z = radius-x;
- print(space(2*z));
- System.out.println("*");
- }
- }
- public String space(int x){
- StringBuffer sb = new StringBuffer();
- for(int i =0;i<=x;i++)
- sb.append(" ");
- return sb.toString();
- }
- public void print(Object o){
- System.out.print(o);
- }
- }
复制代码 呃。打印结果:这尼玛是圆啊。。。与预期相差太大了吧。。
|