0

编写这个程序是为了让用户输入一个从 0 到 10 的数字列表,然后程序确定输入了多少值并输出不同条目的列表以及该条目出现次数的计数。

我正在寻找一种缩短 foreach 索引的方法。

    static void Main(string[] args)
    {
        int [] emptyArray = new int [100];
        string inPut;
        int count= 0;


        Console.WriteLine("Please enter intergers between 0-10: " + "((00 to exit)) ", count + 1);
        inPut= Console.ReadLine();

        while(inPut !="00")
        {
            emptyArray[count]= Convert.ToInt32(inPut);
            ++count;
            Console.WriteLine("Please enter intergers between 0-10: " + "((00 to exit)) ", count + 1);
            inPut= Console.ReadLine();
        }

            int a = 0;
            foreach (int value in emptyArray)
            if (value == 1) ++a;
            Console.WriteLine("The number of times ONE was inputed was {0}",a);

            int b = 0;
            foreach (int value in emptyArray)
            if (value == 2) ++b;
            Console.WriteLine("The number of times TWO was inputed was {0}", b);

            int c = 0;
            foreach (int value in emptyArray)
            if (value == 3) ++c;
            Console.WriteLine("The number of times THREE was inputed was {0}", c);

            int d = 0;
            foreach (int value in emptyArray)
            if (value == 4) ++d;
            Console.WriteLine("The number of times FOUR was inputed was {0}", d);

            int e = 0;
            foreach (int value in emptyArray)
            if (value == 5) ++e;
            Console.WriteLine("The number of times FIVE was inputed was {0}", e);

            int f = 0;
            foreach (int value in emptyArray)
            if (value == 6) ++f;
            Console.WriteLine("The number of times SIX was inputed was {0}", f);

            int g = 0;
            foreach (int value in emptyArray)
            if (value == 7) ++g;
            Console.WriteLine("The number of times SEVEN was inputed was {0}", g);

            int h = 0;
            foreach (int value in emptyArray)
            if (value ==8) ++h;
            Console.WriteLine("The number of times EIGHT was inputed was {0}", h);

            int i = 0;
            foreach (int value in emptyArray)
            if (value == 9) ++i;
            Console.WriteLine("The number of times NINE was inputed was {0}", i);

            Console.Read();
    }
}

}

4

5 回答 5

4

使用查找:

var lookup = emptyArray.ToLookup(i => i);

Console.WriteLine("The number of times ONE was inputed was {0}", lookup[1].Count());
Console.WriteLine("The number of times TWO was inputed was {0}", lookup[2].Count());
/// etc.
于 2012-10-23T16:15:21.253 回答
2
var counts = emptyArray.GroupBy(x => x)
                       .Select(x => new {Key = x.Key, Count = x.Count()});

foreach (var p in counts)
{
    Console.WriteLine("The number of times {0} was inputed was {1}", 
                      p.Key.AsWord(), p.Count);
}

扩展方法 AsWord 在这里定义:

public static class Extensions
{
    public static string AsWord(this int num)
    {
        switch (num) 
        {
            case 1: return "ONE";  
            case 2: return "TWO";
            // ...
            default: throw new ArgumentException("num");
        }
    }
}
于 2012-10-23T16:18:08.730 回答
2

解决方案#1

使用字典计算输入的频率:

string[] digits = new string[]{"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"};

Dictionary<int, int> frequencyMap = new Dictionary<int,int>();

int [] emptyArray = new int[100];

获得输入并填充“emptyArray”后,您可以映射频率:

//... blah blah 
foreach(int value in emptyArray)
{
    if(!frequencyMap.ContainsKey(value))
    {
        frequencyMap.Add(value, 0);
    }
    frequencyMap[value]+=1;
}

// Now print the frequencies
foreach(int key in frequencyMap.Keys)
{
    Console.WriteLine("The number of times {0} was inputed was {1}", digits[key], frequencyMap[key]);
}

解决方案#2

上述解决方案非常强大,如果您不必打印数字,那么您将能够计算任何数字输入,而不必担心打印它。如果您严格落在 0-9 的范围内,那么您可以跳过字典并直接使用数组:

int[] frequencyMap = new int[10];
for(int i = 0; i < frequencyMap.Length; i++)
{
    frequencyMap[i] = 0;
}

foreach(int value in emptyArray)
{
    if(value>0 && value <10)
    {
        frequencyMap[value]+=1;
    }
}

// Now print the frequencies
foreach(int key in frequencyMap)
{
    Console.WriteLine("The number of times {0} was inputed was {1}", digits[key], frequencyMap[key]);
}
于 2012-10-23T16:09:28.180 回答
0

也许您想要 aList<int>而不是int[100]-List<int>将根据实际添加到其中的项目数进行调整,因此foreach将仅返回用户实际输入的项目数。

这闻起来有点像家庭作业,因此您可能需要使用数组。如果这是正确的,我不会给出完整的解决方案,而只是建议您应该使用for循环而不是foreach循环。

于 2012-10-23T16:15:52.367 回答
0

我想有很多方法可以做到这一点。你会被宠坏的选择:) 这是我的

var l = emptyArray.ToLookup (a => a);
var nums = new string[]{"zero","one","two","three","four","five","six","seven","eight","nine"};
foreach (var element in l)
{
    Console.WriteLine ("The number of times " + nums[element.Key] + " was inputed was " + element.Count ());          
}
于 2012-10-23T16:33:49.010 回答