C#保持我的数组中的标记值

时间:2016-02-08 18:49:24

标签: c# arrays sentinel

当我输入10个值时,此代码有效。如果我输入较少,我的哨兵值将被添加。我想要停下来,以及能够操纵我的阵列长度,所以当输入少于10时我不会得到多少0。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace IntegerStatistics
{
class Program
{
    static void Main(string[] args)
    {
        int[] numbers = new int[10];
        int arrayCount, high, low, sum;
        double avg;

        arrayCount = FillArray(numbers);


        Statistics(numbers, arrayCount, out high, out low, out sum, out avg);

        for (int x = 0; x < numbers.Length; ++x)
            Write("{0, 4}", numbers[x]);
        WriteLine();
        WriteLine("The array has {0} values", arrayCount);
        WriteLine("The highest value is {0}", high);
        WriteLine("The lowest value is {0}", low);
        WriteLine("The sum of the array is {0}", sum);
        WriteLine("The average is {0}", avg);

    }

    private static int FillArray(int[] numbers)
    {
         const int QUIT = 999;
        string enterNum;
        int stop;
        int count = 0;
        int addNum = 0;
        stop = numbers.Length - 1;
        while((addNum != QUIT) && (count <= stop))
        {
            Write("Enter a number or 999 to exit: ");
            enterNum = ReadLine();
            while (!int.TryParse(enterNum, out numbers[count]))
                {
                WriteLine("Error");
                Write("Enter a number or 999 to exit: ");
                enterNum = ReadLine();
            }


            numbers[count] = Convert.ToInt32(enterNum);
            addNum = numbers[count];
            ++count;



        }
        return count;


    }

    private static int Statistics(int[] numbers, int arrayCount, out int high, out int low, out int sum, out double avg)
    {
        high = numbers.Max();
        low = numbers.Min();
        sum = numbers.Sum();
        avg = numbers.Average();


        return arrayCount;
    }




}

}

3 个答案:

答案 0 :(得分:3)

首先,让我们修复您的代码,因为它非常简单:不是在numbers.Length中使用Main,而是使用arrayCount。这是你已经拥有的东西,它会阻止Main在最后显示零。

  

我想[...]操纵我的数组长度

虽然.NET provides a way to resize an array,但这不是你应该做的事情,因为你的代码很快就会难以阅读。

解决此问题的更好方法是从FillArray返回一个大小合适的数组。但是,最好的解决方案是切换到使用List<T>,允许根据需要增长和缩小。

答案 1 :(得分:0)

我已修改原始程序以使用List<int>(良好做法)而不是动态调整大小的整数数组(通常没有那么好的做法)。

class Program
{
    static void Main(string[] args)
    {
        // Use a collection instead of an array, as length is as of yet unknown:
        List<int> numbers;

        int high, low, sum;
        double avg;

        numbers = FillArray();

        Statistics(numbers, out high, out low, out sum, out avg);

        foreach (var number in numbers)
        {
            Console.Write("{0, 4}", number);
        }
        Console.WriteLine();
        Console.WriteLine("The array has {0} values", numbers.Count);
        Console.WriteLine("The highest value is {0}", high);
        Console.WriteLine("The lowest value is {0}", low);
        Console.WriteLine("The sum of the array is {0}", sum);
        Console.WriteLine("The average is {0}", avg);

        Console.ReadKey();

    }

    private static List<int> FillArray(int maximum = 10)
    {
        const int QUIT = 999;
        int count = 0;
        int addNum = 0;
        var list = new List<int>();
        while (count <= maximum)
        {
            Console.Write("Enter a number or 999 to exit: ");
            if (!int.TryParse(Console.ReadLine(), out addNum))
            {
                Console.WriteLine("Error");
                continue;
            }
            if (addNum == QUIT)
            {
                break;
            }
            list.Add(addNum);
            count++;
        }
        return list;
    }

    private static void Statistics(List<int> numbers, out int high, out int low, out int sum, out double avg)
    {
        high = numbers.Max();
        low = numbers.Min();
        sum = numbers.Sum();
        avg = numbers.Average();
    }
}

我还注意到您将999的“逃逸”值包含在numbers的集合中。我更正了这一点,以便999不包括在计算的平均值中(我猜这是你的意图)。

答案 2 :(得分:0)

与@Dave类似。允许值为999.没有异常处理,... ...

class Program
    {
        private static string _STOP = "STOP";
        private static int _MAX_SIZE = 10;

        static void Main(string[] args)
        {
            List<int>numbers = FillList();

            foreach(int number in numbers)
                Console.Write("{0, 4}", number);

            Console.WriteLine();
            Console.WriteLine("The list has {0} values", numbers.Count);
            Console.WriteLine("The highest value is {0}", numbers.Max());
            Console.WriteLine("The lowest value is {0}", numbers.Min());
            Console.WriteLine("The sum of the array is {0}", numbers.Sum());
            Console.WriteLine("The average is {0}", numbers.Average());
            Console.ReadKey();

        }

        private static List<int> FillList()
        {
            List<int> numbers = new List<int>();
            int value;
            int count = 0;
            do
            {
                Console.Write("Enter a number or {0} to exit: ", _STOP);
                string line = Console.ReadLine();
                if (line == _STOP)
                    break;

                if (int.TryParse(line, out value))
                {
                    numbers.Add(value);
                    count++;
                }
                else
                {
                    Console.WriteLine("Error reading number.");
                }
            } while (count < _MAX_SIZE);

            return numbers;
        }
    }