數組,一維,二維,多維

數組

 

一維數組數組

         數組初始化,建立數組,數組長度爲5dom

            int [] array=new int[5];排序

            array[0] = 1;索引

            array[1] = 2;數學

            array[2] = 3;string

            array[3] = 4;it

            array[4] = 5;數據類型

         直接賦值的數組數據

            int[] array1 = new int[] {1,2,3,4,5 };二維數組

數組,在建立的時候,須要規定數據類型,初始長度

 

 

二維數組

            int [,] shuzu = new int[3,4];

            3     表示有3個一維數組

            4     表示每個一維數組有4個元素

 

            給第一個一維數組的2號索引賦值

            shuzu[0, 2] = 3;

         直接賦值

            int[,] shuzu1 = new int[,] {

            {1,2,3,4},

            {2,3,4,5},

            {3,4,5,6}

            };

 

冒泡排序

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

            {

                for (int j =i; j < 9;j++ )

                {

                    if(shuzi[i]<shuzi[j+1])

                    {

                       int zhong = shuzi[i];

                       shuzi[i] = shuzi[j + 1];

                       shuzi[j + 1] = zhong;                 

                    }

                }

練習:

輸入人數,輸入每一個人年齡,求總和

 

            // Console.Write("請輸入班級人數:");

            // int n = int.Parse(Console.ReadLine());

            // int [] ban=new int[n];

            //int sum=0;

            // for (int i = 0; i < n; i++)

            // {

            //     Console.Write("請輸入年齡:");

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

            //    sum += ban[i];

            // }

            // Console.WriteLine("年齡的總和是"+sum);

            // Console.ReadLine();

 輸入班級人數,存放語數英成績,求每人總和,語文總分,數學平均分

            //Console.Write("請輸入班級人數:");

            //int n = int.Parse(Console.ReadLine());

            //double[,] chengji = new double[n, 3];

            //double a = 0;

            //double yz = 0;

            //double sp = 0;

            //for (int i = 0; i < n; i++)

            //{

            //    Console.Write("請輸入語文成績:");

            //    chengji[i, 0] = double.Parse(Console.ReadLine());

            //    Console.Write("請輸入數學成績:");

            //    chengji[i, 1] = double.Parse(Console.ReadLine());

            //    Console.Write("請輸入英語成績:");

            //    chengji[i, 2] = double.Parse(Console.ReadLine());

            //}

            //for (int j = 0; j < n; j++)

            //{

            //    a = chengji[j, 0] + chengji[j, 1] + chengji[j, 2];

            //    Console.WriteLine("第{0}我的的三門成績總和是{1}", (j + 1), a);

            //}

            //for (int k = 0; k < n; k++)

            //{

            //    yz += chengji[k, 0];

            //}

            //Console.WriteLine("語文成績的總和是:" + yz);

            //for (int p = 0; p < n; p++)

            //{

            //    sp += chengji[p, 1];

            //}

            //Console.WriteLine("數學平均分紅績的是:" + (sp / n));

            //Console.ReadLine();

 

 

           

打印柴,利用二維數組

            //string[,] mingzi = new string[16, 10]

            //{

            //{"  ","  ","  ","●","  ","  ","","●","",""},

            //{"  ","  ","  ","●","  ","  ","●","  ","  ",""},

            //{"  ","●","  ","●","  ","  ","●","  ","  ","●"},

            //{"  ","●","  ","●","  ","  ","●","  ","●",""},

            //{"  ","●","  ","●","●","●","●","●","",""},

            //{"  ","●","  ","●","  ","  ","●","","",""},

            //{"  ","●","  ","●","  ","  ","●","","","●"},

            //{"●","●","●","●","●","  ","●","●","●",""},

            //{"  ","  ","  ","  ","●","","","","",""},

            //{"  ","  ","  ","  ","●","","","","",""},

            //{"  ","●","●","●","●","●","●","●","●",""},

            //{"  ","  ","  ","●","●","●","","","",""},

            //{"  ","  ","●","  ","●","  ","●","","",""},

            //{"  ","●","  ","  ","●","  ","  ","●","",""},

            //{"●","  ","  ","  ","●","  ","  ","  ","●",""},

            //{"  ","  ","  ","  ","●","  ","  ","  ","  ",""},

            //};

            //for(int i=0;i<16;i++)

            //{

            //for(int b=0;b<10;b++)

            //{

            //Console.Write(mingzi[i,b]);

            //}

            //Console.WriteLine();

            //}

            //Console.ReadLine();

隨機一百之內10個數,按照大小順序排列

 

            int[] shuz = new int[10];

            Random shu = new Random();

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

            {

                int a = shu.Next(101);

                shuz[i] = a;

                Console.WriteLine(shuz[i]);

            }

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

            {

                for (int k = j; k < 9; k++)

                {

                    if (shuz[j] > shuz[k + 1])

                    {

                        int zhong = shuz[j];

                        shuz[j] = shuz[k + 1];

                        shuz[k + 1] = zhong;

                    }

                }

            }

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

            {

                Console.WriteLine(shuz[i]);

            }

            Console.ReadLine();

 

隨機一百之內十個不重複的數,按照大小順序排列。

           // int[] shuz = new int[10];

           //Random shu=new Random();

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

           // {

           //    bool b=true;

           //     int a = shu.Next(101);

           //     shuz[i] = a;

           //     for (int j = 0; j < i; j++)

           //     {

           //         if (a == shuz[j])

           //         {

           //             b = false;

           //             i--;

           //         }

           //         if (b)

           //         {

           //             shuz[i] = a;

           //         }

           //     }

           //     Console.WriteLine(shuz[i]);

           // }

           // for (int j = 0; j < 10; j++)

           // {

           //     for (int k = j; k < 9; k++)

           //     {

           //         if (shuz[j] > shuz[k + 1])

           //         {

           //             int zhong = shuz[j];

           //             shuz[j] = shuz[k + 1];

           //             shuz[k + 1] = zhong;

           //         }

           //     }

           // }

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

           // {

           //     Console.WriteLine(shuz[i]);

           // }

           // Console.ReadLine();

            //Random ran = new Random();

    //輸入每一個人的手機號,,進行滾動顯示,,清屏,隨即一個索引,根據索引打印,等待0.1秒

           // Console.Write("請輸入人數:");

           // int a =int.Parse( Console.ReadLine());

           // string[]zhj=new string[a];

           // for (int i = 0; i < a;i++ )

           // {

           //     Console.Write("請輸入第{0}我的的手機號:",(i+1));

           //     zhj[i] = Console.ReadLine();

           // }

           // Console.WriteLine("全部號碼輸入完畢,請按回車繼續抽獎活動!");

           //Console.ReadLine();

           // Random ran=new Random();

           // for (int j = 0; j < 30;j++ )

           // {

           //     Console.Clear();

           //     int aa = ran.Next(a);

           //     Console.WriteLine("中獎號碼是:"+zhj[aa]);

           //     System.Threading.Thread.Sleep(100);

           // }

           // Console.ReadLine();

相關文章
相關標籤/搜索