以下所有代码均托管在GitHub上面(点击这里直达)
实现效果:接收输入的半径,打印出一个圆
代码如下:
private void printCircle(){
//接收用户输入的半径
Scanner scan = new Scanner(System.in);
System.out.print("请输入圆的半径R(大于3的整数):");
int r = scan.nextInt();
for (int y = 0; y <= 2 * r; y += 2) {
long x = Math.round(r - Math.sqrt(2 * r * y - y * y));
long longLength = 2 * (r - x);
for (int i = 0; i <= x; i++) {
System.out.print(" ");
}
System.out.print("*");
for (double j = 0; j <= longLength; j++) {
System.out.print(" ");
}
System.out.println("*");
}
}
运行结果如下:
请输入圆的半径R(大于3的整数):20
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
希望对你有所帮助