【问题标题】:How to count each occurrence of each element in an array c#如何计算数组中每个元素的每次出现c#
【发布时间】:2014-07-04 23:08:57
【问题描述】:

我正在尝试构建一个程序,其中用户输入一系列数字(整数)并将它们存储在一个数组中,计算每个数字输入的次数并返回每个数字的计数。我还跟踪输入的有效值和无效值的总数,但不必对它们进行排序,只需计数即可。

我的问题在于,按照我的教授的指示,我们还不允许使用 group 子句。我们还没有声明隐式局部变量,他不希望我们使用 var 以及使用列表。

我能够计算有效条目和无效条目的数量,但无法计算数组中每个元素的出现次数。

这是我遇到问题的特定循环,它列出了数组 ex 中的每个位置。 0、1、2,而不是计算每个数字出现的次数。

for (int i = 0; i < values.Length; i++)
Console.WriteLine("Value {0} was entered: {1} time(s)", values[i], i);




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Testing7._1OutAgain
{
class Program
{
    static void Main(string[] args)
    {
        string inputValue;
        int numberOfValues = 0,
            intValue,
            incorrectValues = 0;

        Console.Write("This program contains the following test data and will" +
            " return the values to you, tell you how many total values were inputted" +
            " and tell you how many times each of the values were repeated.\n\n");

        //Get array size
        Console.Write("How many numbers will you enter?\t\n");
        string stringSize = Console.ReadLine();
        int arraySize = Convert.ToInt32(stringSize);
        int[] values = new int[arraySize];


        //Fill array and count valid entry with numberOfValues++, invalid with incorrectValues
        while (numberOfValues < arraySize)
        {

            Console.WriteLine("Enter value: ");
            inputValue = Console.ReadLine();
            intValue = Convert.ToInt32(inputValue);
            if (intValue >= 1 && intValue <= 10)
            {
                values[numberOfValues] = Convert.ToInt32(inputValue);
                numberOfValues++;
            }
            else
            {
                Console.WriteLine("Incorrect value.");
                incorrectValues++;

            }
        }

        for (int i = 0; i < values.Length; i++)
            Console.WriteLine("Value {0} was entered: {1} time(s)", values[i], i);

        Console.WriteLine("\n\nTotal number of values = {0}", numberOfValues);
        Console.WriteLine("\n\nTotal number of incorrect values = {0}", incorrectValues);
        Console.ReadKey();
    }
  }
}

这是我在这里的第一篇文章,我已经查看了针对同一问题的其他解决方案,并且我看到了一些使用 group 子句的出色解决方案,但我不允许这样做。非常感谢您的帮助,我希望这个问题有意义,我只是想成为一个更好的程序员。如果它不是很复杂,我很感激任何帮助并道歉。我是编程新手。

【问题讨论】:

    标签: c# arrays


    【解决方案1】:

    例如,您可以创建一个字典来存储该信息。创建一个字典变量,您可以在其中声明其他变量,如下所示:

    static void Main(string[] args)
    {
        string inputValue;
        int numberOfValues = 0,
            intValue,
            incorrectValues = 0;
    
        Dictionary<int, int> ocurrences = new Dictionary<int, int>();
    
    
        //....rest of code bellow
    }
    

    然后在你的while循环中:

    while (numberOfValues < arraySize)
    {
    
           Console.WriteLine("Enter value: ");
           inputValue = Console.ReadLine();
           intValue = Convert.ToInt32(inputValue);
    
    
           if (intValue >= 1 && intValue <= 10)
           {
                if (!ocurrences.ContainsKey(intValue))
                {
                    ocurrences.Add(intValue, 0);
                }
                values[numberOfValues] = Convert.ToInt32(inputValue);
                numberOfValues++;
    
                ocurrences[intValue]++;
           }
           else
           {
                 Console.WriteLine("Incorrect value.");
                 incorrectValues++;
    
           }
    }
    

    要像这样提取信息:

    //Dont do this one...
    //for (int i = 0; i < values.Length; i++)
         // Console.WriteLine("Value {0} was entered: {1} time(s)", values[i], i);
    
    foreach (KeyValuePair<int, int> pair in ocurrences)
    {
        Console.WriteLine("Number {0} occured {1} times.",pair.Key,pair.Value);
    }
    

    【讨论】:

      【解决方案2】:
         for (int i = 0; i < values.Length; i++)
              Console.WriteLine("Value {0} was entered: {1} time(s)", values[i], i);
      

      你基本上是在你的数组上运行,并说出哪个元素在哪个位置。您似乎想要的是清点您的物品。

      因此,对于每个项目,您需要遍历数组,并计算它出现的次数。您必须将 for 循环中的最后一个 i 更改为另一个循环,或者使用一种遍历数组所有项目的方法,并且每次找到您要查找的项目时(您原来的 @987654323 @) 它将增加一个计数器,然后你将拥有 counter 而不是 i

      我会让你为此编写代码,因为从这一点来看它是微不足道的,毕竟这是你的功课:)

      【讨论】:

        【解决方案3】:

        使用int.TryParse(inputValue, out intValue) 而不是intValue = Convert.ToInt32(inputValue) 以避免错误

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-28
          • 1970-01-01
          • 2014-06-12
          • 2018-10-24
          • 1970-01-01
          • 2019-09-24
          相关资源
          最近更新 更多