用“*”,在屏幕上打印出一个圆形。
1
class
Program
2 {
3 static void Main( string [] args)
4 {
5 int r;
6 Console.WriteLine( " 请输入半径: " );
7 if ( ! int .TryParse(Console.ReadLine(), out r))
8 {
9 Console.WriteLine( " 必须为整数! " );
10 return ;
11 }
12
13 for ( int i = r; i >= r * - 1 ; i -- )
14 {
15 // 计算横坐标(2为扩大左侧系数,否则会是椭圆形)
16 int x = ( int )(Math.Sqrt(r * r - i * i) * 2 );
17 // 打印左边空格
18 for ( int b = 0 ; b < ( int )( 2 * r) - x; b ++ )
19 Console.Write( " " );
20 // 左边*
21 Console.Write( " * " );
22 // 打印右边空格
23 for ( int bb = 0 ; bb < 2 * x; bb ++ )
24 Console.Write( " " );
25 // 右边*
26 Console.WriteLine( " * " );
27 }
28 }
29 }
2 {
3 static void Main( string [] args)
4 {
5 int r;
6 Console.WriteLine( " 请输入半径: " );
7 if ( ! int .TryParse(Console.ReadLine(), out r))
8 {
9 Console.WriteLine( " 必须为整数! " );
10 return ;
11 }
12
13 for ( int i = r; i >= r * - 1 ; i -- )
14 {
15 // 计算横坐标(2为扩大左侧系数,否则会是椭圆形)
16 int x = ( int )(Math.Sqrt(r * r - i * i) * 2 );
17 // 打印左边空格
18 for ( int b = 0 ; b < ( int )( 2 * r) - x; b ++ )
19 Console.Write( " " );
20 // 左边*
21 Console.Write( " * " );
22 // 打印右边空格
23 for ( int bb = 0 ; bb < 2 * x; bb ++ )
24 Console.Write( " " );
25 // 右边*
26 Console.WriteLine( " * " );
27 }
28 }
29 }