日期:2014-05-20  浏览次数:20853 次

疯狂java讲义第三章的用*画近似圆的问题(附自己代码求高手指点)
题目如下:通过通过API文档查询Math类的方法,打印如下所示的近似圆,只要给定不同半径,圆的大小随之发生改变;
  **

  * *

  * *

  * *

  * *

* *

  * *

  * *

  * *

  * *

  * *

  **
我上面画的圆不是很好,但是所有点都画出来了,位置基本如此,我自己写了一段代码,基本能画出圆但是总是不太像,求高手指点呀
Java code

import java.lang.Math;
import java.io.*;
/**
* 绘制近似圆
*/
public class Circle
{
    /**
    * 圆的半径
    * @param r:圆的的半径
    */
    private double r;

    public double getR()
    {
        return r;
    }
    
    public void setR(double R)
    {
        r = R;
    }

    public void draw()
    {
        System.out.print("请输入半径");
        try   
        {   
        //读入半径    
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));   
        r = Double.parseDouble(br.readLine()) ;   
        }   
    catch(IOException e)   
        {   
            e.toString();   
        }
        
        int x1,x2,y;
        String str;
        for (int i = 0; i <= 12; i++)
        {
            if (i == 0)
            {
                y = Math.round((float)(r-Math.cos(Math.toRadians(15*i))*r));
            }else{
                y = Math.round((float)(r-Math.cos(Math.toRadians(15*i))*r))-Math.round((float)(r-Math.cos(Math.toRadians(15*i-15))*r));
            }
            x1 = Math.round((float)(r-Math.sin(Math.toRadians(15*i))*r));
            x2 = Math.round((float)(r+Math.sin(Math.toRadians(15*i))*r));
            str = "";
            if (i != 0 && i != 12)
            {
                if (y == 0)
                {
                    continue;
                }
            }else if(i == 12){
                System.out.println("");
            }
            for (int j=0; j <y; j++)
            {
                System.out.println("");
            }
            for (int k=0; k < x1; k++)
            {
                str+=" ";
            }

            str+="*";

            for (int k=0; k < x2-x1; k++)
            {
                str+=" ";
            }
            
            str+="*";
            System.out.print(str+"角度"+15*i+"y="+y+" x1="+x1+" x2="+x2);
            
        }
        System.out.println("");
        draw();
    }

    public static void main(String args[])
    {
        new Circle().draw();  
    }
    
}



------解决方案--------------------
import java.lang.Math;

public class Circle{
 private int r=10;
 private int initblank;
 private int middleblank;

 public Circle(int r){
this.r=r;
 }

 public void setInitblank(int initblank){
this.initblank=initblank;
 }
 public double getInitblank(){
return initblank;
 }
 
 public void setMiddleblank(int middleblank){
this.middleblank=middleblank;
 }
 public double getMiddleblank(){
return middleblank;
 }

public String getInitblanks(int t){
 String blank=" ";
 
 for(int k=0;k<t-1;k++){
blank=blank+" ";
 }
 return blank;
}

public String getMiddleblanks(int t){
String blank=" ";