缩短foreach指数

时间:2012-10-23 16:06:35

标签: c# foreach

编写该程序,以便用户输入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();
    }
}

}

5 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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]);
}

答案 2 :(得分: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");
        }
    }
}

答案 3 :(得分:0)

也许你想要List<int>而不是int[100] - List<int>会调整实际添加到其中的商品数量,因此foreach只返回用户实际输入的项目数。

这有点像家庭作业,所以你可能需要使用阵列。如果这是正确的,我不会提供完整的解决方案,但只是建议您使用for循环而不是foreach循环。

答案 4 :(得分: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 ());          
}