C#使用2个数组,计算数字在一个数组中出现的次数
所以,是的,目前的任务是制作一个程序,要求用户输入数字。当他们退出时,显示他们输入数字次数的柱状图 要求 数字是0-10。 用户可以随时退出 让用户可以输入任意数量的数字。 像 如果他们输入1,0,0,2,2,3,4,4,5 0*** 1* 2** 3* 4*** 五,* 所以我就做了这个,在发现我误读了这个程序之前,我得到了一个有效的版本。我想我只需要有10个条目,然后显示出来。让它起作用了。然后我发现它基本上应该是无限的条目。我们应该使用两个数组。一个用于数字0-10,一个用于计数。应该在for循环中 我想既然我得到了我认为是我需要做的事情,我的大脑就对如何做到这一点感到茫然 我的假设是 数组[11]{0,1,2,3,4,5,6,7,8,9,10) 计数[11]{0,0,0,0,0,0,0,0,0,0} 然后请求用户输入,检查数字,如果是0,则将count的索引0值增加1。如果是1,则将count的索引增加1,并将其值增加1。依此类推 我不想做一堆if语句,而且我一辈子都想不出如何调用索引来显示一个值。如果我想知道index+3的值是多少(那么,输入了多少次3),我该如何显示它 这是我以前的代码,我确实有一个类用于我调用的东西。GetInput和OutputMessage只是使用控制台读写代码的方法 我不能使用字典或列表。我认为不需要两个数组 谢谢你的帮助C#使用2个数组,计算数字在一个数组中出现的次数,c#,arrays,C#,Arrays,所以,是的,目前的任务是制作一个程序,要求用户输入数字。当他们退出时,显示他们输入数字次数的柱状图 要求 数字是0-10。 用户可以随时退出 让用户可以输入任意数量的数字。 像 如果他们输入1,0,0,2,2,3,4,4,5 0*** 1* 2** 3* 4*** 五,* 所以我就做了这个,在发现我误读了这个程序之前,我得到了一个有效的版本。我想我只需要有10个条目,然后显示出来。让它起作用了。然后我发现它基本上应该是无限的条目。我们应该使用两个数组。一个用于数字0-10,一个用于计数。应该在f
static public void Myhistogram()
{
Histogram myInstanceofDisplayNumbers = new Histogram();
AverageArray myInstanceofHighLowAverage = new AverageArray();
ArrayInputAndSort myInstanceofArrayInputAndSort = new ArrayInputAndSort();
try
{
InputOutput.OutputMessage("Enter 10 numbers from 0-10");
int[] numbers = new int[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int[] counts = new int[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int count = 0;
bool entrycompleted = false;
string input = "a";
while (!entrycompleted)
{
input = InputOutput.GetInput(string.Format("Entry {0}", count + 1));
int result;
if (int.TryParse(input, out result) == false && input.ToLower() != "q" || int.Parse(input) < 0)
{
InputOutput.OutputMessage("Please enter a number from 0-9");
}
else
{
if (input == String.Empty)
{
InputOutput.OutputMessage("Entry was blank");
}
else if (input == "q")
{
InputOutput.OutputMessage("Sorry no quitting until all 10 numbers entered");
}
else
{
if (int.TryParse(input, out result))
//result is the number inputted by the user, it will be asked if its greater or equal to 0, and less than or equal to 10.
// if it is, follow through, otherwise, invalid entry.
{
if (result >= 0 && result <= 10)
{
// numbers is my inputted array
numbers[count] = result;
if (count > 10)
{
entrycompleted = true;
}
}
else
{
InputOutput.OutputMessage("Invalid entry. Try again.");
}
}
else
{
entrycompleted = true;
}
}
}
}
InputOutput.OutputMessage("");
myInstanceofArrayInputAndSort.SortNumbers(numbers.Length, numbers);
myInstanceofDisplayNumbers.DisplayHistogram(numbers, count, counts);
}
catch (Exception ex)
{
//OutputMessage is a static method of the class
//InputOutput therefore you DO NOT need your own
//instance; INSTEAD reference using the classname
InputOutput.OutputMessage(ex.Message);
}
}
public void DisplayHistogram(int[] numbers, int count, int[] counts)
{
// int[] frequencyofintegers = new int[10];
for (int i = 0; i < count; i++)
{
counts[numbers[i]]++;
}
for (int i = 0; i <= 10; i++)
{
if (counts[i] == 0)
{
}
else
{
InputOutput.OutputSameLine(string.Format("{0}: ", i));
for (int j = 0; j < counts[i]; j++)
{
InputOutput.OutputSameLine("*");
}
InputOutput.OutputMessage("");
}
}
InputOutput.OutputMessage("");
}
static public void Myhistogram()
{
直方图MyInstanceOfDisplayNumber=新直方图();
AverageArray myInstanceofHighLowAverage=新AverageArray();
ArrayInputAndSort myInstanceofArrayInputAndSort=新的ArrayInputAndSort();
尝试
{
OutputOutput.OutputMessage(“输入0-10之间的10个数字”);
int[]number=新的int[10]{0,0,0,0,0,0,0,0,0};
int[]计数=新的int[10]{0,0,0,0,0,0,0,0,0};
整数计数=0;
bool entrycompleted=false;
字符串输入=“a”;
而(!entrycompleted)
{
input=InputOutput.GetInput(string.Format(“条目{0}”,计数+1));
int结果;
if(int.TryParse(input,out result)==false&&input.ToLower()!=“q”| | int.Parse(input)<0)
{
OutputOutput.OutputMessage(“请输入0-9之间的数字”);
}
其他的
{
if(输入==String.Empty)
{
OutputOutput.OutputMessage(“条目为空”);
}
else if(输入=“q”)
{
OutputOutput.OutputMessage(“对不起,在输入全部10个数字之前,请勿退出”);
}
其他的
{
if(int.TryParse(输入、输出结果))
//结果是用户输入的数字,询问是否大于等于0,是否小于等于10。
//如果是,则执行,否则为无效条目。
{
如果(结果>=0&&10)
{
entrycompleted=true;
}
}
其他的
{
InputOutput.OutputMessage(“无效条目。请重试”);
}
}
其他的
{
entrycompleted=true;
}
}
}
}
InputOutput.OutputMessage(“”);
MyInstanceFarrayInputandSortNumber(number.Length,number);
显示直方图(数字、计数、计数);
}
捕获(例外情况除外)
{
//OutputMessage是类的静态方法
//因此,您不需要自己的输入输出
//实例;而不是使用类名引用
InputOutput.OutputMessage(例如Message);
}
}
公共无效显示直方图(int[]数字、int计数、int[]计数)
{
//int[]积分频率=新的int[10];
for(int i=0;i 对于(int i=0;i由于需求允许0-10,您已经有了索引!您不需要第二个数组、字典或其他任何东西
因此,您的输入读数如下(简化):
要打印出来,只需循环:
for (int i = 0; i < 11; i++)
Console.WriteLine("{0}, {1}", i, counts[i]);
for(int i=0;i<11;i++)
WriteLine(“{0},{1}”,i,counts[i]);
为了得到一个具体的计数,它只是counts[i]
谷歌搜索会有帮助,但这应该会给你一个想法@DJKRAZE,如果他在计数之前存储了输入,这会有所帮助。你也可以实时计数。这也是事实。Bradleydotnett谢谢你,我很感激,它在看了这个之后点击了。
for (int i = 0; i < 11; i++)
Console.WriteLine("{0}, {1}", i, counts[i]);