2018-12-19问题三

2018-12-19  本文已影响0人  吾_5c00

int[] arry = { 1, 3, -1, 5, -2 };

        int[] newArry = new int[arry.Length];   //存储新元素的数组

        int j = -1; //用于标识新数组的下标

        for (int i = arry.Length - 1; i >= 0; i--)
        {
            j++;
            if (arry[i] > 0)
            {
                newArry[j] = arry[i];
            }
            else
            {
                newArry[j] = 0;
            }
        }
        Console.WriteLine("原先数组的值:");
        foreach (var item in arry)
        {
            Console.Write(item + "\t");

        }
        Console.WriteLine();
        Console.WriteLine("新数组的值:");
        foreach (var item in newArry)
        {
            Console.Write(item + "\t");

        }
        Console.ReadKey();
上一篇下一篇

猜你喜欢

热点阅读