函数笔记

    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)都是值类型,他们默认情况下会传值。

 

传址

       把实参的地址传组函数的形参中去。形参与实参共用同一个数据空间。

       一般引用类型都是传地址的。

练习,青歌赛

    static viod Main(string [] arg)
   {
             //青歌赛 10人打分去一个最高去一个最低取平均值
            //输入
            int [] cj= new int [10];
           cj =Shu();
           int max = Max(cj);
           int min = Min(cj);
           int sum = Sum(cj);
           int fs = Chu(Max(cj),Min(cj),Sum(cj));
           Console.WriteLine("选手得分"+fs);
        }
        static int[] Shu() //输入
        {
            int [] a=new int [10];
            for (int i = 0; i < 10;i++ )
            {
                Console.WriteLine("请给选手打分");
                a[i] = Convert.ToInt32(Console.ReadLine());
            }
            return a;
        }
        static int Max(int []a)  //最大值
        {
            int zd = 0;
            for (int i = 0; i < a.Length;i++ )
            {
                if (zd < a[i])
                {
                    zd = a[i];
                }
            }
            return zd;
        }
        static int Min(int[] a)  //最小值
        {
            int zx = 0;
            for (int i = 0; i < a.Length; i++)
            {
                if (zx > a[i])
                {
                    zx = a[i];
                }
            }
            return zx;
        }
        static int Sum(int [] a)   //
        {
            int sum = 0;
            for (int i = 0; i < a.Length;i++ )
            {
                sum = sum + a[i];
            }
            return sum;
        }
        static int Chu( int max, int min,int sum)  //分数
        {     
            
             int jg=(sum-max-min)/8;
             return jg;
        }

二分法查找

static void Main(string[] args)
        {
            //给定一个元素查找在一维数组存不存在
            int [] a=new int [10] {33,12,45,66,11,78,99,122,97,88};
            Console.WriteLine("请输入一个数字");
            int n = Convert.ToInt32(Console.ReadLine());
             b = Paixu(a);
            bool jg = Chazhao( b,n);
            if (jg = true)
            {
                Console.WriteLine("有");
            }
            else
            {
                Console.WriteLine("木有");
            }


        }
        static int[] Paixu(int [] x)
        {
            for (int i = 0; i < x.Length;i++ )
            {
                for (int j = 0; j < x.Length - i;j++ )
                {
                    if (x[i]>x[j+1])
                    {
                        int kong = 0;
                        kong = x[i];
                        x[i] = x[j + 1];
                        x[j + 1] = kong;
                    }

                }
                return x;
            }
        
        }
        static bool Chazhao(int[] c,int mb)
        {
            int zj; int ks=0; int js=c.Length-1;
            bool ok = true;
            for (; ; )
            {
                zj = (ks + js) / 2;
                if (c[zj] > mb)
                {
                    js = zj-1;

                }
                if (c[zj] < mb)
                {
                    ks = zj + 1;
                }
                if (c[zj]==mb)
                {
                    ok = true;
                    
                    break;
                }
                else if (js < ks)
                {
                    ok = false;
                    
                    break;
                }
            }
            return ok;
        }

  

原文地址:https://www.cnblogs.com/zoubizhici/p/5435045.html