C# 函数重载 示例 求圆的面积

依然范特西╮ 提交于 2020-04-08 13:24:05
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    
class Circle
    
{
       
        
private const float PI = 3.141526F;

        
//1.没有任何已知条件
        public static double Area()
        

            Console.WriteLine(
"空空如也!");
            
return 0;
        
        }

        
//2.已知圆心坐标
        public static double Area(int x1, int y1)
        
{
            Console.WriteLine(
"这是一个圆点,坐标为({0},{1})",x1,y1);
            
return 0;
        }

        
//3.已知半径
        public static double Area(double r)
        
{
            
double theArea;
            theArea 
= PI * r * r;
            
return theArea;

        }

        
//4.已知圆心坐标和半径
        public static double Area(int x1, int y1, double r)
        
{
            Console.WriteLine(
"这是一个圆点在({0},{1})半径为{2}的圆", x1, y1, r);
            
return Area(r);
        
        }

        
//5.已知圆心和圆周边上的一点
        public static double Area(int x1, int y1, int x2, int y2)
        
{
            
int x = x2 - x1;
            
int y = y2 - y2;
            
double r = (double)Math.Sqrt(x*x+y*y);
            Console.WriteLine(
"这是一个圆心在({0},{1}),圆周边一点在({2},{3})的圆,圆的半径为{4}",x1,y1,x2,y2,r);
            
return Area(r);
        
        }

        
static void Main(string[] args)
        
{
            
int x1=2,x2=4;          //x坐标
            int y1=3,y2=5;          //y坐标
            double radius=3;        //半径
            double CircleArea = 0;
            CircleArea 
= Area();
            Console.WriteLine(
"-->1.面积为{0}",CircleArea);
            Console.WriteLine();

            CircleArea 
= Area(x1,y1);
            Console.WriteLine(
"-->2.面积为{0}", CircleArea);
            Console.WriteLine();

            CircleArea 
= Area(radius);
            Console.WriteLine(
"-->3.面积为{0}", CircleArea);
            Console.WriteLine();

            CircleArea 
= Area(x1, y1, radius);
            Console.WriteLine(
"-->4.面积为{0}", CircleArea);
            Console.WriteLine();

            CircleArea 
= Area(x1, y1, x2, y2);
            Console.WriteLine(
"-->5.面积为{0}", CircleArea);
            Console.WriteLine();

            Console.ReadKey();

        }

       
    }

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!