函数

函数: 数学,三角函数,f(x) ……
能够独立完成某项功的模块。
函数四要素:输入,输出,函数体,函数名

函数的定义,函数的调用——大家都做过了。

一、函数定义
[static] 返回类型 函数名([形参类型 形参名[,形参类型 形参名]])
{
函数体
}


二、函数调用
[变量类型 变量名 = ]函数名(实参值)

例题:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Class1
{
static int[] ShuRu(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
Console.Write("请第"+(i+1)+"个评委亮分:");
a[i] = Convert.ToInt32(Console.ReadLine());
}
return a;
}

static int[] PaiXu(int[] a)
{
for (int i = 1; i <= a.Length - 1; i++)
{
for (int j = 1; j <= a.Length - i; j++)
{
if (a[j - 1] < a[j])
{
int temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
}
}
}

return a;
}
static string MaxMin(int[] a)
{
string s = "去掉"+Max(a)+",去掉"+Min(a);
return s;
}
static string Max(int[] a)
{
string s = "两个最高分"+Max1(a)+"和"+Max2(a);
return s;
}
static string Min(int[] a)
{
string s = "两个最低分" + Min1(a) + "和" + Min2(a);
return s;
}
static int Max1(int[] a)
{
return a[0];
}
static int Max2(int[] a)
{
return a[1];
}
static int Min1(int[] a)
{
return a[a.Length - 1];
}
static int Min2(int[] a)
{
return a[a.Length - 2];
}
static double QiuZhi(int[] a)
{
int sum = 0;
for (int i = 2; i <= a.Length - 3; i++)
{
sum = sum + a[i];
}
double avg = 1.0*sum / (a.Length - 4);
return avg;
}

static void Main(string[] args)
{
int[] a = new int[10];
//输入
a = ShuRu(a);
//运算
//1.排序
a = PaiXu(a);
//2.求值
double d = QiuZhi(a);
//输出
//Console.WriteLine("去掉两个最高分"+Max1(a)+"和"+Max2(a)+",去掉两个最低分"+Min1(a)+"和"+Min2(a)+",选手的平均得分是:" + d);
Console.WriteLine(MaxMin(a)+",选手的平均得分是:"+d);
}
}
}

posted on 2015-05-27 20:40  rongzichuan  阅读(155)  评论(0编辑  收藏  举报