天天看點

C#複習之數組

歡迎來到unity學習、unity教育訓練、unity企業教育訓練教育專區,這裡有很多U3D資源、U3D教育訓練視訊U3D教程、U3D常見問題、U3D項目源碼,我們緻力于打造業内unity3d教育訓練、學習第一品牌。

今天我們來一起複習一下數組。

兩道題帶你們領悟數組:

 編寫一個程式,從鍵盤輸入10個整數并儲存到數組裡,求出這10個整數中最大、最小和平均值

首先定義一個數組,定義最大值,最小值,平均值

要求輸入10個數,是以要用readline,之後進行比較。

代碼如下:

class Num

    {

        int[] a = new int[10];

        int max,min;

        int sum = 0;

        float avg = 0.0f;  

        public void Avg()

        {

            for (int i = 0; i < 10; i++)

            {

                a[i] = int.Parse(Console.ReadLine());

            }

            for (int i = 0; i < 10; i++)

            {

                sum = sum + a[i]; 

            }

            avg = sum / 10.0f;

            Console.WriteLine(avg);

            for (int i = 0; i < 10; i++)

            {

                if (a[i]>max)

                {

                    max = a[i];

                }

            }

            Console.WriteLine(max);

            min=a[0];

            for (int i = 0; i < 10; i++)

            {

                if (a[i] < min)

                {

                    min = a[i];

                }

            }

            Console.WriteLine(min);

        }

        static void Main(string[] args)

        {

            Num n = new Num();

            n.Avg();

            Console.ReadKey();

        }

    }

求數列1,1,2,3,5,8…的前20項

首先定義一個數組,

要注意數組下标,推出通式,列印

代碼如下:

class Add

    {

        int[] a = new int[20];

        public void Writ()

        {

            a[0] = 1;

            a[1] = 1;

            Console.WriteLine(a[0]);

            Console.WriteLine(a[1]);

            for (int i = 2; i < 20; i++)

            {

                a[i] = a[i - 1] + a[i - 2];

                Console.WriteLine(a[i]);

            }

        }

        static void Main(string[] args)

        {

            Add c = new Add();

            c.Writ();

        }

    }

今天學習感悟:

         首先祝大家平安夜快樂,今天我的同桌回來了,終于有人全程幫我解決我那些低智商的問題了,哈哈,例如5+7等于幾... 特别感謝我們組的人,真的特别感謝,希望我可以跟的上以後的課程,可以繼續和他們一起學習!我會努力的!加油!