Java画近似圆(疯狂Java讲义第四章)新手(请多指教)
代码
import java.io.*;
public class Test3{
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputStr = null;
System.out.print("请输入圆的半径:");
inputStr = br.readLine();
//圆的半径
double r = Double.parseDouble(inputStr);
System.out.println("==========打印圆==========");
//遍历所有经过y轴的直线
for(int y = 0;y <= 2*r;y = y + 2)
{
//以y轴分割,直线与圆相交的左边点的值xl
double xl = Math.round(r - Math.sqrt(Math.pow(r,2) - Math.pow((r - y),2)));
以y轴分割,直线与圆相交的右边点的值xr
double xr = Math.round(r + Math.sqrt(Math.pow(r,2) - Math.pow((r - y),2)));
//打印xl左边的空格
for(int i = 0;i <= (int)xl;i++)
{
System.out.print(" ");
}
//打印xl
System.out.print("*");
//打印xr左边的空格,由于xl与其左边的空格占了xl(空格) + 1(自身),因此,空格数 = xr - xl -1
for(int i = 0;i <= (int)xr - (int)xl - 1;i++)
{
System.out.print(" ");
}
//打印xr
System.out.println("*");
}
}
}