【发布时间】: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 子句的出色解决方案,但我不允许这样做。非常感谢您的帮助,我希望这个问题有意义,我只是想成为一个更好的程序员。如果它不是很复杂,我很感激任何帮助并道歉。我是编程新手。
【问题讨论】: