zoukankan      html  css  js  c++  java
  • foreach使用和函数

    2016-04-25

    一.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》 //哈希表只能用foreach去遍历。

     

    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)下的子级集合。

    6.foreach使用范围较小,不能取代for

     

    2.函数:

    主要的功能就是为了让代码结构更加良好。

    函数是——实现相对独立功能的程序代码模块(程序段).

    函数的四要素:函数名,输入,输出,运算

     

     

    有的函数没有输入,函数名后的小括号中可以不写东西,但必须要有小括号。

    有的函数没有返回,在函数名的左侧不要写数据类型了,写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.青歌赛打分:

     

    namespace ConsoleApplication1
    {
        class Class2
        {
            static void Main(string[] arge)
            {
               //青歌赛打分,10个评委,最大值,最小值,去掉最高分和最低分的平均分
                int []a=new int [10];
                int max = 0, min = 100, sum = 0;
                //输入
                //运算
                a = shuru(a);
                max =Max(a );
                    min=Min (a);
                    sum = Sum(a);
    
                
                //输出
                    shuchu(a, max, min, sum);
            }
            static void shuchu(int[] a,int max,int min,int sum)
            {
                double avg = 1.0*(sum - max - min) / (a.Length - 2);
                Console.WriteLine("最高分是{0}	 最低分是{1}	平均分{2}",max,min,avg);
            }
            static int[] shuru(int[] a)
            {
                for (int i = 0; i < a.Length;i++ )
                {
                    Console.WriteLine("第{0}位评委评分是:",i+1);
                    a[i] = Convert.ToInt32(Console .ReadLine ());
                }
                return a;
            }
            static int Max(int[] a)
            {
                int zd = 0;
                foreach (int b in a )
                {
                if (zd <b)
                {
                    zd = b;
                }
                }
                return zd ;
            }
            static int Min(int[] a)
            {
                int zx = 100;
                foreach (int b in a)
                {
                if (zx>b)
                {
                    zx=b;
                }
                }
                return zx;
            }
            static int Sum(int[] a)
            {
                int he = 0;
                foreach (int b in a)
                {
                    he = he + b;
                }
                return he;
            }
    
        }
    }

    2.  题目:给定一个数组,给出一个数,查找一下是否存在,用二分法

    class Class3
        {
            static void Main(string[] ssdgrd)
            {
                //给定一个数组,给出一个数,查找一下是否存在
                int[] a = new int[] {23,85,65,15,20,95,36,3,14,12 };
            //输入
                Console.WriteLine("输入要查找的数:");
                int n = Convert.ToInt32(Console .ReadLine ());
                //排序
                a=paixu(a);
    
                //查找
                bool zhaodaole = chazhao(a, n);
                //输出
            if (zhaodaole ==true )
            {
                Console.WriteLine("找到啦");
            }
                if (zhaodaole ==false )
                {
                    Console.WriteLine("木找到");
                }
            }
            static int[] paixu(int[] a)
            {
                for (int i = 0; i < a.Length;i++ )
                {
                    for (int j = i; j < a.Length - 1;j++ )
                    { 
                    if (a[i]>a[j+1])
                    {
                        int t = a[i];
                    a[i]=a[j+1];
                    a[j + 1] = t;
                    }
                    }
                }
                return a;
            }
            static bool chazhao(int [] a,int n)
            {
                bool find = false;
                int start=0,end=a.Length -1,mid;
                for (; ; )
                { 
                    mid =(start+end )/2;
                    if (n == a[mid])
                    {
                        find = true;
                        break;
                    }
                    else
                    { 
                    if (n>a[mid])
                    {
                        start = mid + 1;
                    }
                        if (n<a[mid])
                        {
                            end = mid - 1;
                        }
                        else if (end <start )
                        {
                            find = false;
                            break;
                        }
                    }
                }
                return find;
            }

     

     

     

     

     

     

     

  • 相关阅读:
    php责任链模式
    php工厂模式
    php观察者模式
    php单例模式
    php的抽象类
    Mysqli的常用函数
    PDO的基本操作
    算法--各种算法
    file_get_post实现post请求
    redis的5种数据结构的使用场景介绍
  • 原文地址:https://www.cnblogs.com/pangchunlei/p/5429263.html
Copyright © 2011-2022 走看看