foreach 循环遍历 以及函数的应用

foreach( 对集合每个元素的引用 in 集合 )
{

}

举例:

int[] a = new int[5]{1,2,3,4,5};
foreach( int b in a )
{
//b就是a中的每个元素
}

注意:
1.foreach只能对集合进行遍历。
2.foreach在操作集合的时候,只能读不能改;

3.foreach操作Dictionary<T,T>

Dictionary<string,string> dic = new Dictionary<string,string>();
//...添加值

foreach( KeyValuePare<string,string> b in dic)
{
}

4.foreach操作List<T>

List<int> list = new List<int>();
//添加值
foreach( int b in list )
{
}

5.foreach能够操作IEnumarable(IEnumarator)下的子级集合。

函数:
主要的功能就是为了让代码结构更加良好。
函数是——实现相对独立功能的程序代码模块(程序段).
函数的四要素:函数名,输入,输出,运算

有的函数没有输入,函数名后的小括号中可以不写东西,但必须要有小括号。
有的函数没有返回,在函数名的左侧不要写数据类型了,写void即可.

定义语法:
static 返回类型 函数名(形参列表)
{
函数体,运算
}

static int Max(int a,int b)
{
  if(a>b)
  {
      return a;
  }
  else
  {
      return b;
  }
}


调用语法:
数据类型 变量名 = 函数名(实参列表);
int n = Max(7,8);


形参与实参一定要一一对应(个数,类型)

传值
把实参的数据做个复本,送到函数的形参中去。
一般基本类型(int,double,char,bool,DateTime)都是值类型,他们默认情况下会传值。

举例:

 1 static void Main(string[] args)
 2         {
 3             int a = 10;
 4             Console.WriteLine(a);//???  10
 5             Add(a);
 6             Console.WriteLine(a);//???  10--实参的值传到调用的Add函数里,实参值并不改变,int型默认传值
 7         }
 8         static void Add(int x)
 9         {
10             Console.WriteLine(x);//???  10
11             x++;
12             Console.WriteLine(x);//???  11
13         }

运行结果:

传址
把实参的地址传组函数的形参中去。形参与实参共用同一个数据空间。
一般引用类型都是传地址的,如数组和字符串

举例:

 1 static void Main(string[] args)
 2         {
 3             int[] a = new int[3] {3,4,5 };
 4             //显示
 5             Show(a);            //3  4  5
 6             Add(a);
 7             //显示                //数组默认的是传址,就是将地址传到所调用的函数里面,实参的值也会跟着改变
 8             Show(a);           //103  104  105 
 9         }
10         static void Show(int[] x)
11         {
12             foreach (int b in x)
13             {
14                 Console.Write(b+"	");
15             }
16             Console.WriteLine();
17         }
18         static void Add(int[] x)
19         {
20             //显示
21             Show(x);            //3  4  5
22             for (int i = 0; i < x.Length; i++)
23             {
24                 x[i] = x[i] + 100;
25             }
26             //显示                //103  104  105
27             Show(x);
28         }

运行结果:


练习题:

1.调用函数:青歌赛打分:20位评委给一个选手打分,去掉一个最高分,去掉一个最低分求歌手的平均得分。

 1      static void Main1(string[] args)
 2         {
 3             //调用函数:青歌赛打分:20位评委给一个选手打分,去掉一个最高分,去掉一个最低分求歌手的平均得分。
 4 
 5             //定义数组接收评委的分数
 6             int[] fenshu=new int[20];
 7             int max = 0, min = 10000,sum=0;
 8 
 9             //打分
10             fenshu = Shuru(fenshu);
11 
12             //运算
13             max = Max(fenshu);
14             min = Min(fenshu);
15             sum = Sum(fenshu);
16             //输出
17             Shuchu(fenshu,max,min,sum);
18        
19         }
20         static int [] Shuru(int[]a)//#######输入函数
21         {
22             for (int i = 0; i <a.Length; i++)
23             {
24                 Console.Write("请第{0}个评委打分:",i+1);
25                 a[i] = Convert.ToInt32(Console.ReadLine());
26             }
27             return a;
28         }
29         static int Max(int[] a)//#######求最大值函数
30         {
31             int zuida = 0;
32             foreach (int b in a)
33             {
34                 if (b>zuida)
35                 {
36                     zuida = b;
37                 }
38             }
39             return zuida;
40         }
41         static int Min(int[] a)//#######求最小值函数
42         {
43             int zuixiao = 10000;
44             foreach (int b in a)
45             {
46                 if (b<zuixiao)
47                 {
48                     zuixiao = b;
49                 }
50             }
51             return zuixiao;
52         }
53         static int Sum(int[] a)//#######求和函数
54         {
55             int sum = 0;
56             for (int i = 0; i <a.Length; i++)
57             {
58                 sum = sum + a[i];
59             }
60             return sum;
61         }
62         static void Shuchu(int[] a,int max,int min,int sum)//######输出函数
63         {
64             double avg =1.0* (sum - max - min) / (a.Length - 2);
65             Console.WriteLine("去掉一个最高分{0},去掉一个最低分{1},该选手的平均得分是:{2}",max,min,avg);
66         }

2.顺序查找法:输入一个数,看是否能在已知数组中找到,输出找到或没找到

 1  static void Main2(string[] args)
 2         { 
 3             //*******顺序查找法******* 
 4 
 5             int[] a=new int[]{12,23,33,45,65,42,78,66};
 6             //输入
 7             int n = Convert.ToInt32(Console.ReadLine());
 8             //查找
 9             bool find = found(a,n);
10           
11 
12             //输出
13             if (find==true)
14             {
15                 Console.WriteLine("找到了");
16             }
17             else
18             {
19                 Console.WriteLine("没找到");
20             }
21         }
22         
23         static bool found(int[] a, int n)
24         {
25             bool found = false;
26             foreach (int b in a)
27             {
28                 if (b==n)
29                 {
30                     found = true;
31                     break;
32                 }
33             }
34             return found;
35         }

3.二分法查找,题目同上

 1 static void Main3(string[] args)
 2         { 
 3         //*******二分法查找******
 4             int[] a = new int[] { 12, 23, 33, 45, 65, 42, 78, 66 };
 5             //输入
 6             int n = Convert.ToInt32(Console.ReadLine());
 7 
 8             //排序
 9             a = Shunxu(a);
10             //查找(二分法)
11             bool find = zhaodao(a,n);
12 
13             //输出
14             if (find==true)
15             {
16                 Console.WriteLine("找到了");
17             }
18             else
19             {
20                 Console.WriteLine("没找到");
21             }
22 
23         }
24         static int[] Shunxu(int[] a)//排序函数,从小到大排序
25         {
26             for (int i = 1; i <= a.Length - 1; i++)
27             {
28                 for (int j = 1; j <= a.Length - i; j++)
29                 {
30                     if (a[j] < a[j - 1])
31                     {
32                         int z = a[j];
33                         a[j] = a[j - 1];
34                         a[j - 1] = z;
35                     }
36                 }
37             }
38             return a;
39         }
40         static bool zhaodao(int[] a, int n)//查找函数
41         {
42             bool zhaodaole = false;
43             int ks = 0, js = a.Length - 1, zj;
44             for (; ; )
45             {
46                 
47                 zj = (ks + js) / 2;
48                 if (a[zj]==n)
49                 {
50                      
51                     zhaodaole = true;
52                     break;
53 
54                 }
55                 else
56                 {
57                     if (a[zj]>n)
58                     {
59                         js = zj - 1;
60                     }
61                     else
62                     {
63                         ks = zj + 1;
64                     }
65                     if (ks<js)
66                     {
67                         break;
68                     }
69                 }
70                 
71             }
72             return zhaodaole;
73         }
原文地址:https://www.cnblogs.com/kellybutterfly/p/5428523.html