首页 > 解决方案 > 如何找到数组的最小值和最大值以及数组的平方和反转的数组?

问题描述

我正在尝试编写一个代码,该代码将为我提供数组中的最高和最低数字以及数组平方和反转数组。当我运行程序时,我一直得到 0,但没有其他问题,所以设置为错误,但我无法弄清楚是什么问题,因为没有错误消息。这是代码:

using System;

namespace ArrayMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter an array size: ");
            int someArray = Convert.ToInt32(Console.ReadLine());


            int[] Array = new int[someArray];

            String input = Console.ReadLine();

            int[] arrayPart3 = new int[10];

            ReversedArray(arrayPart3);
            for (int index = 0; index < arrayPart3.Length; index++)
            {
                Console.Write(arrayPart3[index] + "| ");
            }
            Console.WriteLine();

            Console.WriteLine("Largest is:" + ArrayMax(arrayPart3));
            Console.WriteLine("Smallest is:" + ArrayMin(arrayPart3));

            SquaredArray(arrayPart3);
            for (int index = 0; index<arrayPart3.Length; index++)
            {
                Console.Write(arrayPart3[index] + "| ");
            }
            Console.WriteLine();

            ReversedArray(arrayPart3);
            for (int index = 0; index < arrayPart3.Length; index++)
            {
                Console.Write(arrayPart3[index] + "| ");
            }

        }

        public static int ArrayMax(int[] someArray)
        {
            int highest = someArray[0];
            for (int index = 0; index < someArray.Length; index++)
            {
                if (someArray[index] > highest)
                    highest = someArray[index];
            }
            return highest;
        }

        public static int ArrayMin(int[] someArray)
        {
            int lowest = someArray[0];
            for (int index = 0; index < someArray.Length; index++)
            {
                if (someArray[index] < lowest)
                    lowest = someArray[index];
            }
            return lowest;
        }

        public static void SquaredArray(int[]someArray)
        {

            for (int index = 0; index < someArray.Length; index++)
            {
                 someArray[index] = someArray[index] * someArray[index];
            }

        }


        public static void ReversedArray(int[] someArray)
        {
            for (int index = 0; index<someArray.Length; index++)
            {
                int temp = someArray[index];
                someArray[index] = someArray[someArray.Length - 1 - index];
                someArray[someArray.Length - 1 -index] = temp;
            }

        }
    }
}

标签: c#

解决方案


缺少解析输入的代码

这是一个基于 linq 的紧凑代码:

using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var input = Console.ReadLine();
        // or use a fixed input: var input = "1 2 3 4";
        int[] arrayPart3 = input.Split(' ',',', ';').Select(s => int.Parse(s)).ToArray();

        Console.WriteLine("Reversed: " + string.Join("| ", arrayPart3.Reverse()));
        Console.WriteLine();

        Console.WriteLine("Largest is:" + arrayPart3.Max());
        Console.WriteLine("Smallest is:" + arrayPart3.Min());
        Console.WriteLine("Squared: " + string.Join("| ", arrayPart3.Select(i => i*i)));
        Console.WriteLine("Reversed Squared: " + string.Join("| ", arrayPart3.Reverse().Select(i => i*i)));

    }
}

推荐阅读