题目如下:通过通过API文档查询Math类的方法,打印如下所示的近似圆,只要给定不同半径,圆的大小随之发生改变;
** * * * * * * * ** * * * * * * * * * * * **
我上面画的圆不是很好,但是所有点都画出来了,位置基本如此,我自己写了一段代码,基本能画出圆但是总是不太像,求高手指点呀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;
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();
}
}
请输入半径3
**角度0y=0 x1=3 x2=3
* *角度45y=1 x1=1 x2=5
* *角度60y=1 x1=0 x2=6
* *角度90y=1 x1=0 x2=6
* *角度105y=1 x1=0 x2=6
* *角度120y=1 x1=0 x2=6
* *角度150y=1 x1=2 x2=5
**角度180y=0 x1=3 x2=3
请输入半径4
**角度0y=0 x1=4 x2=4
* *角度30y=1 x1=2 x2=6
* *角度60y=1 x1=1 x2=7
* *角度75y=1 x1=0 x2=8
* *角度90y=1 x1=0 x2=8
* *角度105y=1 x1=0 x2=8
* *角度120y=1 x1=1 x2=7
* *角度135y=1 x1=1 x2=7
* *角度165y=1 x1=3 x2=5
**角度180y=0 x1=4 x2=4
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=" ";
for(int k=0;k<t-1;k++){
blank=blank+" ";
}
return blank;
}
public static void main(String[] args){
double pi=java.lang.Math.PI;
double basepi=pi/6;
Circle circle=new Circle(5);
for(int i=0;i<=6;i++){
double countpi=i*basepi;
// System.out.println(java.lang.Math.sin(countpi));
// System.out.println(java.lang.Math.sin(java.lang.Math.PI));
circle.middleblank=(int)(circle.r*(java.lang.Math.sin(countpi)));
circle.initblank=(int)(circle.r-circle.middleblank);
if( circle.middleblank==0.0)
System.out.println(circle.getInitblanks(circle.initblank)+"*");
else if( circle.initblank==0.0)
System.out.println("*"+circle.getMiddleblanks(circle.middleblank)+circle.getMiddleblanks(circle.middleblank)+"*");
else
System.out.println(circle.getInitblanks(circle.initblank)+"*"+circle.getMiddleblanks(circle.middleblank)+circle.getMiddleblanks(circle.middleblank)+"*");
}
}
}
把一个圆分成左右半圆然后从上到下用30多切割然后计算X轴坐标点(姑且这么认为),但是你默认Y轴坐标点每次递减1,也就是你默认圆的X轴半径可变,而Y轴半径始终为6。这样的话,如果R=100,还能算是近似圆吗?
但是还是感谢,你上面的主要代码我就笑纳了哈哈。