C#使用2个数组,计算数字中出现的数字的次数

时间:2014-10-23 21:41:16

标签: c# arrays

所以是的,目前的任务是制作一个要求用户输入数字的程序。当他们退出时,显示他们输入数字的次数的直方图

要求 数字是0-10。 用户可以随时退出 使用户可以输入任意数量的数字。 显示它 如果他们输入1,0,0,0,2,2,3,4,4,4,5 0 *** 1 * 2 ** 3 * 4 *** 5 *

所以我曾经为此工作过,并且在发现我误读了该程序之前得到了它的工作版本。我以为我只需要有10个条目,并显示出来。有那个工作。然后我发现它应该是无限制的条目基本上。我们应该使用两个数组。一个用于数字0-10,一个用于计数。应该在for循环中。

我认为,因为我得到了我认为我需要做的事情,我的大脑正在知道如何做到这一点。

我的假设类似于

数组[11] {0,1,2,3,4,5,6,7,8,9,10) 计数[11] {0,0,0,0,0,0,0,0,0,0,0}

然后询问用户输入,检查数字,如果是0,则将计数的索引0值增加1.如果为1,则将计数索引增加1并将其值增加1.等等< / p>

我不想制作一堆if语句,而且我不能在我的生活中想到如何将索引称为显示值。如果我想看看Index + 3的值是什么(那么,输入了多少次3)我该如何显示它。

这是我的旧代码,我确实有一个课程,我呼吁的东西。 GetInput和OutputMessage只是使用控制台读写代码的方法。

我不能使用词典或列表。我不认为需要2个数组。

感谢您提供任何帮助。

    static public void Myhistogram()
    {
        Histogram myInstanceofDisplayNumbers = new Histogram();
        AverageArray myInstanceofHighLowAverage = new AverageArray();
        ArrayInputAndSort myInstanceofArrayInputAndSort = new ArrayInputAndSort();

        try
        {
            InputOutput.OutputMessage("Enter 10 numbers from 0-10");

            int[] numbers = new int[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            int[] counts = new int[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            int count = 0;
            bool entrycompleted = false;
            string input = "a";

            while (!entrycompleted)
            {

                input = InputOutput.GetInput(string.Format("Entry {0}", count + 1));
                int result;
                if (int.TryParse(input, out result) == false && input.ToLower() != "q" || int.Parse(input) < 0)
                {
                    InputOutput.OutputMessage("Please enter a number from 0-9");

                }
                else
                {
                    if (input == String.Empty)
                    {
                        InputOutput.OutputMessage("Entry was blank");
                    }
                    else if (input == "q")
                    {
                        InputOutput.OutputMessage("Sorry no quitting until all 10 numbers entered");

                    }





                    else
                    {
                        if (int.TryParse(input, out result))
                            //result is the number inputted by the user, it will be asked if its greater or equal to 0, and less than or equal to 10. 
                            // if it is, follow through, otherwise, invalid entry. 
                        {
                            if (result >= 0 && result <= 10)
                            {
                                // numbers is my inputted array
                                numbers[count] = result;



                                if (count > 10)
                                {
                                    entrycompleted = true;
                                }
                            }
                            else
                            {
                                InputOutput.OutputMessage("Invalid entry. Try again.");
                            }

                        }
                        else
                        {
                            entrycompleted = true;
                        }

                    }
                }
            }
            InputOutput.OutputMessage("");

            myInstanceofArrayInputAndSort.SortNumbers(numbers.Length, numbers);
            myInstanceofDisplayNumbers.DisplayHistogram(numbers, count, counts);

        }
        catch (Exception ex)
        {
            //OutputMessage is a static method of the class
            //InputOutput therefore you DO NOT need your own
            //instance; INSTEAD reference using the classname
            InputOutput.OutputMessage(ex.Message);
        }

    }

    public void DisplayHistogram(int[] numbers, int count, int[] counts)
    {

       // int[] frequencyofintegers = new int[10];


        for (int i = 0; i < count; i++)
        {
            counts[numbers[i]]++;
        }

        for (int i = 0; i <= 10; i++)
        {
            if (counts[i] == 0)
            {

            }
            else
            {

                InputOutput.OutputSameLine(string.Format("{0}: ", i));

                for (int j = 0; j < counts[i]; j++)
                {
                    InputOutput.OutputSameLine("*");
                }

                InputOutput.OutputMessage("");
            }
        }

        InputOutput.OutputMessage("");
    }

1 个答案:

答案 0 :(得分:1)

由于要求允许0-10,您已经有了索引!你不需要第二个数组,字典或其他任何东西。

所以你的输入读取看起来像(简化):

counts[int.Parse(Console.ReadLine())]++; //Probably should use TryParse!

要打印出来,只需循环:

for (int i = 0; i < 11; i++)
   Console.WriteLine("{0}, {1}", i, counts[i]);

要获得具体计数,只需counts[i]

相关问题