[Java] 纯文本查看 复制代码
import java.util.Scanner;
public class Circle2 {
//定义一个字符串类型的二维数组来存储圆形
public static String[][] circle;
public static int radius;
public static void main(String[] args){
while(true){
System.out.println("请输入需要打印的圆的半径:");
Scanner sc = new Scanner(System.in);
radius = sc.nextInt();
if(radius > 20){
System.out.println("超出范围了!!!");
break;
}
String[][] circle = new String[(radius << 1) + 1][radius << 2];
init(circle);
initCircle(circle);
printCircle(circle);
}
}
public static void init(String[][] circle){
for(int i = 0; i < circle.length; i++){
for(int j = 0; j < circle[i].length; j++){
circle[i][j] = " ";
}
}
}
public static void printCircle(String[][] circle){
for(int i = 0; i < circle.length; i++){
for(int j = 0; j < circle[i].length; j++){
System.out.print(circle[i][j]);
}
System.out.println();
}
}
public static void initCircle(String[][] circle){
for(int i = 0; i < circle.length; i++){
double angle = Math.acos((radius * 1.0 - i) / (radius + 0.1));
int point1_y = (int)(1 + (radius << 1) - Math.sin(angle) * (radius << 1));
int point1_x = i;
int point2_y = circle[0].length - point1_y;
int point2_x = i;
circle[point1_x][point1_y] = "*";
circle[point2_x][point2_y] = "*";
}
}
}