C#Lesson04

2021-03-12 19:35发布

C#基础   第四天

#region 选择排序
            //int[] a = {49,38,65,97,76,13,27 };
            //for (int i = 0; i < a.Length-1; i++)
            //{
            //    for (int j = i+1; j < a.Length; j++)
            //    {
            //        //j=1,2,3,4,5,6
            //        if (a[i]>a[j])
            //        {
            //            int temp = a[i];
            //            a[i] = a[j];
            //            a[j] = temp;
            //        }
            //    }
            //}
            //foreach (var item in a)
            //{
            //    Console.Write(item+",");
            //}

            //double[] arr = {12,90,14,84,34,9,23,55,10.5,8 };
            //for (int i = 0; i < arr.Length-1; i++)
            //{
            //    for (int j = i+1; j < arr.Length; j++)
            //    {
            //        if (arr[i]>arr[j])
            //        {
            //            double temp = arr[i];
            //            arr[i] = arr[j];
            //            arr[j] = temp;
            //        }
            //    }
            //}
            //foreach (var item in arr)
            //{
            //    Console.Write(item+",");
            //}

            #endregion



            #region 二维数组
            //有两下标的数组称为二维数组
            //int[] a;
            ////动态初始化
            //int[,] b = new int[2, 3]
            //{ {1,2,3},{3,4,5} };
            //b = new int[,] { { 2,3,4}, {3,4,5 } };
            //int[,] c = { { 2,3,4}, {3,5,7 } };
            //Console.WriteLine(b[1,0]);
            //b[1, 1]=7;
            ////int i = 0;
            ////b[i,0],b[i,1],b[i,2]
            //for (int i = 0; i < b.GetLength(0); i++)
            //{
            //    for (int j = 0; j < b.GetLength(1); j++)
            //    {
            //        Console.Write(b[i,j]);
            //    }
            //    Console.WriteLine();
            //}
            //?将一个二维数组的行和列交换
            //int[,] c = { { 2, 4, 5 }, { 3, 7, 8 } };
            //int[,] d = new int[c.GetLength(1), c.GetLength(0)];
            //for (int i = 0; i < d.GetLength(0); i++)
            //{
            //    for (int j = 0; j < d.GetLength(1); j++)
            //    {
            //        d[i, j] = c[j, i];
            //        Console.Write(d[i, j]);
            //    }
            //    Console.WriteLine();
            //}

            //for (int i = 0; i < c.GetLength(1); i++)
            //{
            //    for (int j = 0; j < c.GetLength(0); j++)
            //    {
            //        d[i, j] = c[j, i];
            //        Console.Write(d[i, j]);
            //    }
            //    Console.WriteLine();
            //}

            //int[,] a = new int[4, 3];
            //int sum = 0;
            //for (int i = 0; i <a.GetLength(0); i++)
            //{
            //    for (int j = 0; j < a.GetLength(1); j++)
            //    {
            //        Console.WriteLine("请输入第{0}行第{1}列元素:",i,j);
            //         a[i,j] = int.Parse(Console.ReadLine());
            //    }

            //}
            //for (int k = 0; k < a.GetLength(1); k++)
            //{
            //    Console.Write(a[0,k]+",");
            //}
            //Console.WriteLine();
            //for (int w = 0; w < a.GetLength(1); w++)
            //{
            //    sum += a[1, w];
            //}
            //Console.WriteLine(sum);
            #endregion



            #region foreach
            //int[] a = { 1,2,3,4,5};
            //int[,]b= { {1,2,3 },{2,3,4 } };
            //int sum = 0;
            //foreach (var item in a)
            //{
            //    //求和
            //    sum += item;
            //    Console.WriteLine(item);
            //}
            //求数组之积
            //int[] intArr = { 1,2,3,4,5};
            //int sum = 1;
            //foreach (var item in intArr)
            //{

            //    sum *= item;
            //}
            //Console.WriteLine(sum);
            //strint str="Hello,Offcn!";用foreach遍历字符串,求字符串中包含几个'l'字符
            //int num = 0;
            //string str = "Hello,Offcn!";
            //foreach (var item in str)
            //{
            //    if (item=='l')
            //    {
            //        num++;
            //    }
            //}
            //Console.WriteLine(num);
            #endregion