java 控制台 画圆

时间:2021-10-25 22:02:12
//实心圆
import java.util.Scanner;  
class circle   
{  
    public static void main(String[] args)   
    {  
           new circle().print();   
     }    
    //画圆函数
    void print() {
        System.out.println("输入半径:");  
        Scanner input=new Scanner(System.in);  
        int r=input.nextInt(); 
        System.out.println("输出:");  
        for(int y=0; y<=2*r;y+=2){  

            int x=(int)Math.round(r-Math.sqrt(r*r-(r-y)*(r-y)));  
            int l=2*(r-x);  
            for(int i=0;i<=x;i++){  
                System.out.print(" ");  
            }  
            System.out.print(".");

            for(int i=0;i<=l;i++){  
                System.out.print("*");  
            }  
            System.out.println("*");  
    }
    }  
}  
//空心圆
import java.util.Scanner;  
class circle2   
{  
    public static void main(String[] args)   
    {  
           new circle2().print();

     }  

    //画圆函数
    void print() {
        System.out.println("输入半径:");  
        Scanner input=new Scanner(System.in);  
        int r=input.nextInt();

        System.out.println("输出:");  
        for(int y=0; y<=2*r;y+=2){  

            int x=(int)Math.round(r-Math.sqrt(r*r-(r-y)*(r-y)));

            int l=2*(r-x);  
            for(int i=0;i<=x;i++){  
                System.out.print(" ");  
            }  
            System.out.print("*");

            for(int i=0;i<=l;i++){  
                System.out.print("*");  
            }  
            System.out.println("");  
    }
    }  
}