二維數組、多維數組

二維數組:數組

定義二維數組 int[,] myArray = new int[幾個一維數組,數組中的個數];ide

 數組能夠具備多個維度。例如,下列聲明建立一個四行兩列的二維數組(能夠理解爲4個1維數組,數組中包含2個元素):spa

int[,] myArray = new int[4,2];3d

int[,] myArray = new  int[4,2] {{1,2}, {3,4}, {5,6}, {7,8}};code

取值則是blog

Int i=myArray[0,0]數學

輸出結果i爲:1string

Int i=myArray[0,1]it

輸出結果i爲:2event

 

多維數組:

數組能夠具備多個維度。例如:

int[,,] myArray = new  int[2,4,2] {{1,2},{3,4},{5,6},{7,8}} , {{9,10},{11,12},{13,14},{15,16}};

一個元素是一個點 一維數組是一條線 二維數組一個面(表格) 三維數組是一個教學樓(立體)

split()  

以***進行分割
分割開的內容須要放置在string類型的數組中,不須要給數組定義長度
Console.Write("請輸入姓名-年齡-工做單位:");
            //"張三-33-漢企"
string s = Console.ReadLine() ;
string[] array = s.Split('-');
foreach(string aa in array)
{
   Console.WriteLine(aa);
 }

上面是一位數組的一個題,前面作過,此次用的是一個數組!!

輸入班級人數,輸入每一個人的語數英成績求語文兩個最高分,數學兩個最低分,英語平均分
            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] = z1;
                    }
                }
            }
            Console.WriteLine("語文最高分爲:" + s[0, 0] + "," + s[1, 0]);
            for (int i = 0; i < a - 1; i++)
            {
                for (int j = i + 1; j < a; j++)
                {
                    if (s[i, 1] > s[j, 1])
                    {
                        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] = z1;
                    }
                }
            }
            Console.WriteLine("數學最低分爲:" + s[0, 1] + "," + s[1, 1]);
            double sum = 0;
            for (int i = 0; i < a; i++)
            {
                sum += s[i, 2];
            }
            Console.WriteLine("英語平均分爲:" + sum / a);
            Console.ReadLine();
View Code

注意冒泡循環中,隨着想要獲得的分數互換的同時,也要把同一數組的其餘項也互換,否則會影響其餘結果。

 查找替換問題
            Console.Write("春眠不覺曉,到處聞啼鳥。夜來風雨聲,花落知多少。\n");
            Console.Write("請輸入要替換的文字:");
            string a = Console.ReadLine();
            Console.Write("請輸入要替換爲的文字:");
            string b = Console.ReadLine();
            string[] s = new string[24] { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
            for (int i = 0; i < 24; i++)
            {
                if (s[i] == a)
                {
                    s[i] = b;
                }
            }
            Console.Write("替換成功,替換後爲:");
            foreach (string aa in s)
            {
                Console.Write(aa);
            }
            Console.ReadLine();
方法一

 

            string a = "春眠不覺曉,到處聞啼鳥。夜來風雨聲,花落知多少。";
            Console.Write("請輸入要替換的字:");
            string b = Console.ReadLine();
            Console.Write("請輸入替換爲的文字:");
            string c = Console.ReadLine();
            Console.WriteLine(a.Replace(b, c));
            Console.ReadLine();
方法二
相關文章
相關標籤/搜索