unity游戏开发-C#语言基础篇(一维数组)

 class Program
    {
        static void Main(string[] args)
        {
             // 第一种声明方式 存放类型[] 数组名字
            int[] arr = new int[] {1,2,3,4,5 };//数组放入元素;固定好类型不能放字符串或其他类型的元素;
            int[] arr1 = new int[5] { 1, 2, 3, 4, 5 };//固定数组长度后面就不能改变;
            int[] arr2 = new int[5];//重新new数组;改变长度;
            arr2 = new int[3] ;
            arr2[arr2.Length-1] = 22;//给数组下标数组赋值;最后一个元素; 
            for (int i = 0; i < arr.Length;i++ )
            {
                Console.WriteLine(arr[i]);
            }
            Console.WriteLine(arr2[arr2.Length - 1]);

            int[] array = new int[] { };//数组整形初始化格式;
            string[] Str = new string[] { };//数组字符串格式;
            double[] Double = new double[] { };//数组小数点格式;


           // 练 数组格式
            int[] ArrayInt = new int[] { };
            string[] arrStr = new string[] { };
            double[] arrDou = new double[] { };


            int[] arrInt = new int[]{ }; 
            string[] arrName=new string[4]{"万","孙","黄","高"};
            for(int i=0;i 5 };
            Console.WriteLine(ArrBool[0]);
            Console.WriteLine(ArrBool[1]);
            Console.WriteLine(ArrBool[2]);
            Console.WriteLine("\n");
            foreach (bool Bool in ArrBool)
            {
                Console.WriteLine(Bool);
            }

            Console.WriteLine("\n");

            foreach (bool Bools in ArrBool)
            {
                if (Bools)
                {
                    Console.WriteLine(Bools);
                }
            }

            int[] ArrInt = new int[] { };
            ArrInt = new int[50];
            for (int i = 0; i < ArrInt.Length; i++)
            {
                ArrInt[i] = 2 * i + 1;

            }
            foreach (int Int in ArrInt)
            {

                Console.WriteLine(Int);
            }

            //练习
            int[] arrInt = new int[] { };
            int[] MyInt = new int[24];
            int j = 0;
            arrInt = new int[50];
            for (int i = 0; i < arrInt.Length; i++)
            {
                arrInt[i] = 2 * i + 1;
                if (arrInt[i] % 5 == 0 || arrInt[i] % 3 == 0)
                {
                    MyInt[j] = arrInt[i];
                    j++;
                }
            }
            Console.Write(j);
            Console.WriteLine("\n这些数为:");
            foreach (int arrInt1 in MyInt)
            {
                Console.Write(arrInt1 + " ");

            }




            Console.WriteLine("\n\n偶数为:");
            int[] arr = new int[] { };
            arr = new int[100];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = i + 1;
                if (arr[i] % 2 == 0)
                {
                    Console.Write(arr[i] + " ");

                }

            }

            Console.WriteLine("\n\n 小数为:");

            double[] arrDou = new double[] { };
            arrDou = new double[10];
            for (int i = 0; i < arrDou.Length; i++)
            {
                arrDou[i] = i + 0.123;
            }
            foreach (double Dou in arrDou)
            {
                Console.WriteLine(Dou);
            }


            Console.WriteLine("100以内奇数被5或3整除");
            int[] arrInt = new int[] { };
            arrInt = new int[50];
            int num = 0;
            for (int i = 0; i < arrInt.Length; i++)
            {
                arrInt[i] = 2 * i + 1;
                if (arrInt[i] % 3 == 0 || arrInt[i] % 5 == 0)
                {
                    num++;
                }
            }


            int[] arrInt1 = new int[num];
            int j = 0;
            for (int i = 0; i < arrInt.Length; i++)
            {

                if (arrInt[i] % 3 == 0 || arrInt[i] % 5 == 0)
                {
                    arrInt1[j] = arrInt[i];
                    j++;
                }
            }
            foreach (int arrInt2 in arrInt1)
            {
                Console.WriteLine(arrInt2);
            }
            Console.WriteLine("\n\n" + num);

            Console.ReadKey();
        }
      
    }

你可能感兴趣的:(unity游戏开发-C#语言基础篇(一维数组))