C#-day5-函数

函数
定义:一个方法是把一些相关的语句组织在一起,用来执行一个任务的语句块。
每个C#程序至少有一个带Main()方法的类,要使用方法,需要
  • 定义方法
  • 调用方法
定义静态方法的格式:
访问修饰符 static 返回值类型 方法名(参数列表){ 代码块; 返回值;
定义静态方法:
定义 无返回值无参数列表 的静态方法:
public static void Test()
{
Console.Write("这是代码块");
}
public static void Main(){
Test();//调用方法
}
>>结果:这是代码块
定义 无返回值有参数 的静态方法:
public static void Test(int count)
{
for(int i=0;i<count ;i++){
Console.Write("这是代码块");
}
}
public static void Main(){
Test(2);//调用方法
}

>>结果:
这是代码块
这是代码块
定义 有返回值有参数 的静态方法:
public static int GetSum(int num)
{
int sum = 0;
for (int i = 1; i <= num; i++)
{
sum += i;
}
return sum;
} //1.GetSum(); 计算1 +...+ 100的和

练习题训练
1.GetSum(); 计算1 +...+ 100的和
public static int GetSum(int num)
{
int sum = 0;
for (int i = 1; i <= num; i++)
{
sum += i;
}
return sum;
}
static void main()
{
Console.WriteLine(GetSum(100));
}

2.GetSub(); 计算1 -。。。。-100的差
public static int GetSub(int num)
{
//2.GetSub(); 计算1 -。。。。-100的差;
int sub = 1;
if (num>1)
{
for (int i = 2; i <= num; i++)
{
sub -= i;
}
}
return sub;
}
static void main()
{
Console.WriteLine(GetSub(3));
}
3.求三角形的面积 GetTriangleArea(int a,int b,int c);
public static double GetTriangleArea(int a, int b, int c)
{
//防止出现负数
if (a <= 0 || b <= 0 || c <= 0)
{
return 0;
}
//是否不满足三角形
if (a + b <= c || a + c <= b || b + c <= a)
{
return 0;//返回一个值同时结束函数,跳出函数
}
double len = (a + b + c) / 2;
double s = len * (len - a) * (len - b) * (len - c);
return Math.Sqrt(s);//开平方
}
static void main()
{
Console.WriteLine("三角形面积"+ GetTriangleArea(1, 4, 5));
}
4.GetMaxNum(double a,double b,double c) 求最大值,返回当前最大值
public static double GetMaxNum(double a, double b, double c)
{
double max = a > b ? a : b;
max = max > c ? max : c;
return max;
}
static void main()
{
Console.WriteLine(GetMaxNum(12, 13, 15);
}
5.IndexOf(int[] arr,int value) 从0开始检索value,检索到第一次出现的value就结束检索,并返回该元素的下标;该元素value检索不到就返回-1;
例如:IndexOf(new int[]{1,3,2,3,4},3);
返回1;
public static int IndexOf(int[] arr, int value)
{
for (int i = 0; i < arr.Length; i++)
{
if (arr[i]==value)
{
return i;
}
}
return -1;
}
static void main()
{
Console.WriteLine(IndexOf(new int[] { 1, 3, 2, 3, 4 }, 3));
}
6.IndexOf(int[] arr,int value,int startIndex), 从startindex处开始往后检索,返回value第一次出现的下标;检索失败就返回-1;
例如:IndexOf(new int[]{1,3,2,3,4},3,2);
返回3;
public static int IndexOf(int[] arr, int value, int startIndex)
{
for (int i = startIndex; i < arr.Length; i++)
{
if (arr[i]==value)
{
return i;
}
}
return -1; //检索失败就返回-1;例如:IndexOf(new int[]{1,3,2,3,4},3,2);
}
static void main()
{
Console.WriteLine(IndexOf(new int[] { 1, 3, 2, 3, 4 }, 3, 2));
}
7.SubArray(int[] arr,int startIndex);从startIndex这个下标开始往后截取所有的元素存储到新数组中,并返回;
例如:SubArray(new int[]{12,13,14,15,16},2);
返回{14,15,16}
public static Array SubArray(int[] arr, int startIndex)
{
if (arr.Length-1<startIndex||startIndex<0)// 判断此下标是否小于0,或者大于目标数组的最大下标
{
return null;
}
int []newArr=new int[arr.Length-startIndex];
int index = 0;//定义一个新数组的索引
for (int i = startIndex; i <arr.Length ; i++)
{
newArr[index] = arr[i];
index++;
}
return newArr;
}
static void main()
{
if (SubArray(new int[] { 12, 13, 14, 15, 16 }, 2)!=null)//判断数组是否为空
{
foreach (var i in SubArray(new int[] { 12, 13, 14, 15, 16 }, 2))
{
Console.Write(i + " ");
}
}
else
{
Console.WriteLine("数组为空");
}
}
8.Select(int[] arr,int value)已知数组是排好序的(从小到大),现在在这个数组中快速查找元素value;并返回value的下标;(必须利用二分法计算)
例如:Select(new int[]{1,3,14,15,16,17,17,17,19},17);
返回:6;
public static int Select(int[] arr, int value)
{
int start = 0;
int final = arr.Length - 1;
while ( start<=final)
{
int middle = (start + final) / 2;
if (arr[middle]<value)
{
start = middle + 1;
}
else if(arr[middle]>value)
{
final = middle - 1;
}
else
{
return middle;
}
}
return 0;
}
static void main()
{
Console.WriteLine(Select(new int[] { 1, 3, 14, 15, 16, 17, 17, 17, 19 }, 17));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值