我需要编写一个程序,将数据读入int类型的数组中。有效值为0到10.您的程序应确定输入的值的数量。 输出不同条目的列表以及该条目发生次数的计数 。“
我遇到了粗体部分的问题,这是我到目前为止所做的......
static void Main(string[] args)
{
Console.WriteLine("How many scores will you enter?");
int total = 0;
string inValue;
string Size = Console.ReadLine();
int arraySize = Convert.ToInt32(Size);
int [] ScoreArray = new int [arraySize];
for (int i = 0; i < ScoreArray.Length; i++)
{
Console.Write("Enter a Number between 0 and 10, #{0}: ", i + 1);
inValue = Console.ReadLine();
ScoreArray[i] = Convert.ToInt32(inValue);
total += ScoreArray[i];
}
Console.WriteLine("Number of scores entered: " + arraySize);
Console.Read();
}
答案 0 :(得分:0)
我认为这里的关键是有效值介于0-10 之间。我会使用数组索引来存储每个值本身。例如,如果处理值5,则递增values[5]
。
首先,你要初始化一个数组,如:
int[] values = new int[11]; //Values 0-10
然后,循环直到用户输入空白:
while(true)
{
string inValue = Console.ReadLine();
if(String.IsNullOrEmpty(inValue)) break;
values[Convert.ToInt32(inValue)]++; //Bounds checking would be nice here
}
然后,您可以通过循环数组一次并打印出值大于0的任何索引来显示已排序的不同列表:
for(int i = 0; i < values.length; i++)
{
if(values[i] > 0)
{
Console.WriteLine("Value {0} was entered {1} time(s)..", i, values[i]);
}
}
这很可能是您的教授所寻求的。我没有测试上面的代码,这是你的工作:)
答案 1 :(得分:0)
此代码可满足您的所有需求:
static void Main(string[] args)
{
//input data
int[] inputArray = new int[5];
int enteredCount = 0;
string enteredLine = string.Empty;
Console.WriteLine("Enter numbers from 0 to 10. If you want to end please enter nothing");
//while user input something not blank
while ((enteredLine = Console.ReadLine()) != string.Empty)
{
//inputArray has no more elements, resize it
if (enteredCount == inputArray.Length)
{
Array.Resize<int>(ref inputArray, inputArray.Length + 5);
}
//new entered value to array
inputArray[enteredCount] = int.Parse(enteredLine);
enteredCount++;
}
//now we need count all uniq elements
//special array. Index is a number from 0 to 10. Value is a count of that value
int[] counts = new int[11];
for (int i = 0; i < enteredCount; i++)
{
int enteredNumber = inputArray[i];
counts[enteredNumber]++;
}
Console.WriteLine("Totally were entered {0} numbers", enteredCount);
//now we now count of each number from 0 to 11. Let' print them
for (int i = 0; i < 11; i++)
{
//Print only numbers, that we entered at least once
if (counts[i] != 0)
Console.WriteLine("Number {0} were entered {1} times", i, counts[i]);
}
Console.ReadLine();
}
答案 2 :(得分:0)
确保添加
using System.Collections.Generic;
using System.Linq;
然后
Console.WriteLine("Distinct values entered & No. of times values entered:");
int[] distinctvalues = ScoreArray.Distinct().OrderBy(x => x).ToArray();
//Finds distinct values and orders them in ascending order.
int[] distinctcount = ScoreArray.FindAllIndexof(distinctvalues).ToArray();
//Finds how many times distinct values are entered.
for (int i = 0; i < distinctvalues.Length; i++)
Console.WriteLine(distinctvalues[i].ToString() + " --------- Entered " +
distinctcount[i].ToString() + " times");
Console.Read();
对于FindAllIndexof
函数,在Extension Method
类
static class
中创建Program
public static class EM
{
public static int[] FindAllIndexof<T>(this IEnumerable<T> values, T[] val)
{
List<int> index = new List<int>();
for (int j = 0; j < val.Length; j++)
index.Add(values.Count(x => object.Equals(x, val[j])));
return index.ToArray();
}
}
输出