编写这个程序是为了让用户输入一个从 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();
}
}
}