二維數組與多維數組以及練習題

二維數組 數組


                int[,]array=new int[3,2]{
             {1,2},
            {3,4},
            {5,6}};
            3表示有三個一維數組。2表示每個一維數組有兩個元素spa

若是把一維數組看做是一個點,那麼二維數組就是一個面索引

多維數組,數學

多維數組就是[,,],的形式 。若是把二維數組看做是一個面,那麼多位數組就是一個立體string

基本瞭解就行,不多用到多維數組it

下面咱們來看幾個用二維數組作的練習題foreach

——————————————————————————————————————————————————————-———————————————二維數組

例一,請輸入班級人數,而後輸入每一個人的語文數學和英語成績,而且打印出語文的兩個最高分,數學的兩個最低分。和英語的平均分英語

 

  Console.Write("請輸入班級人數");
            int a = int.Parse(Console.ReadLine());
            double[,] s = new double[a, 3];
            for (int i = 0; i < a; i++)
            {
                Console.Write("請輸入第{0}我的的語文成績", (i + 1));
                s[i, 0] = double.Parse(Console.ReadLine());
                Console.Write("請輸入第{0}我的的數學成績", (i + 1));
                s[i, 1] = double.Parse(Console.ReadLine());
                Console.Write("請輸入第{0}我的的英語成績", (i + 1));
                s[i, 2] = double.Parse(Console.ReadLine());
            }
            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write(s[i, j] + "\t");
                }
                Console.WriteLine();
            }
            for (int i = 0; i < a - 1; i++)
            {
                for (int j = i+1; j < a; j++)
                {
                    if (s[i, 0] < s[j, 0])
                    {
                        double z = s[i, 0];
                        s[i, 0] = s[j, 0];
                        s[j, 0] = z;
                        double z1=s[i,1];
                        s[i,1]=s[j,1];
                        s[j,1]=z1;
                        double z2=s[i,2];
                        s[i,2]=s[j,2];
                        s[j,2]=z2;new

                    }

                }
            }
            Console.Write(s[0, 0]+","+s[1,0]);


            Console.WriteLine();

            for (int i = 0; i < a - 1; i++)
            {
                for (int j = i + 1; j < a; j++)
                {
                    if (s[i, 0] < s[j, 0])
                    {
                        double z = s[i, 0];
                        s[i, 0] = s[j, 0];
                        s[j, 0] = z;
                        double z1 = s[i, 1];
                        s[i, 1] = s[j, 1];
                        s[j, 1] = z1;
                        double z2 = s[i, 2];
                        s[i, 2] = s[j, 2];
                        s[j, 2] = z2;

                    }

                }
            }
            Console.WriteLine(s[(a - 1), 1] + "," + s[(a - 2), 1]);
            double d = 0;
            for (int i = 0; i < a; i++)
            {
                d += s[i, 2];
            }
            Console.WriteLine(d / a);
            Console.ReadLine();

————————————————————————————————————————————————————————————————————————————————

例二輸入圖形或者漢字。

 

string[,] a = new string[13, 9]
            {        
              {"  ","  ","啦","啦","啦","啦","啦","啦","啦"},
              {"  ","  ","啦","  ","  ","啦","  ","  ","啦"},
              {"  ","  ","啦","  ","  ","啦","  ","  ","啦"},
              {"  ","  ","啦","  ","  ","啦","  ","  ","啦"},
              {"  ","  ","啦","  ","  ","啦","  ","  ","啦"},
              {"  ","  ","啦","  ","  ","啦","  ","  ","啦"},
              {"  ","  ","啦","啦","啦","啦","啦","啦","啦"},
              {"  ","  ","啦","  ","  ","啦","  ","  ","啦"},
              {"  ","  ","啦","  ","  ","啦","  ","  ","啦"},
              {"  ","  ","啦","  ","  ","啦","  ","  ","啦"},
              {"  ","  ","啦","  ","  ","啦","  ","  ","啦"},
              {"  ","  ","啦","  ","  ","啦","  ","  ","啦"},          
              {"  ","  ","啦","啦","啦","啦","啦","啦","啦"},
            };
            for (int i = 0; i < 13; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Console.Write(a[i, j]);
                }
                Console.WriteLine();
            }
            Console.ReadLine();

———————————————————————————————————————————————————————————————————————————————   

例三

請輸入

春眠不覺曉,

到處聞啼鳥。

夜來風雨聲,

花落知多少。

這首詩。而且將鳥字替換爲貓字。

最後打印。替換成功,替換後爲,打出替換後的詩句。

            Console.WriteLine("春眠不覺曉,到處聞啼鳥。夜來風雨聲,花落知多少。");
            Console.WriteLine("請輸入要替換的字");
            string c = Console.ReadLine();
            Console.WriteLine("請輸入替換後的文字");
            string d = Console.ReadLine();
            string[] b = new string[24] { "春", "眠", "不", "覺", "曉", ",", "處", "處", "聞", "啼", "鳥", "。", "夜", "來", "風", "雨", "聲", ",", "花", "落", "知", "多", "少", "。" };
            for (int i = 0; i < 24; i++)
            {
                if (b[i] == c)
                {
                    b[i] = d;
                }


            }
            Console.Write("替換成功,替換後爲:");
            foreach (string cc in b)
            {

                Console.Write(cc);
            }


            Console.ReadLine();

————————————————————————————————————————————————————————————————————————————————

例四

請隨機輸入20個數。將偶數索引值的數放入另外一個數組

            int[] a = new int[20];
            int[] b = new int[10];
            for (int i = 0; i < 20; i++)
            {
                Console.Write("請輸入第 {0}個數;", i + 1);
                a[i] = int.Parse(Console.ReadLine());
            }
            for (int i = 0; i < 20; i += 2)
            {

                b[i / 2] = a[i];
            }
            foreach (int dd in b)
            {
                Console.WriteLine(dd);

            }            Console.ReadLine();

相關文章
相關標籤/搜索