本文介绍了对于循环任务,请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

Hi!

我正在尝试创建100个从11到20的随机数,然后使用水平条形图和垂直条形图对每个频率进行计数.这就是我走的路,但是有些地方不对.请帮我在哪里 我错了!

I am trying to create 100 random numbers ranging from 11 to 20 then count the frequencies of each number with a horizontal bar chart and a vertical bar chart for the frequencies. This is how far I got, but something is not right. Please help me where I went wrong!

static void Main(string[] args)
        { // create 100 random numbers ranging 11-20. Count the frequency of each generated number.
            Random rnd = new Random();
            int max = 0;
            int i,j,x;
            int[] f = new int[10];
            for ( i=0; i<100; i++)
            {
                x = rnd.Next(11, 21); //ranging from 11 to 20, so put 21
                Console.Write(x + " ");
                f[x - 11]++; //count is always added by 1. subtract 11 because the difference is always 11. g
            }
            Console.Write("\n Frequencies: ");
            for (i = 0; i < f.Length; i++)
            {
                Console.Write(f[i]+" ");
            }
            //print a horizontal bar chart
            Console.WriteLine("\n FRQ Table:");
            for (i=0; i<f.Length;i++)
            {
                Console.WriteLine("    {0,2}   {1,2}", i+11, f[i]);
            }
            //print a vertical bar chart for  the frequencies
            //find max frequency
            foreach (int y in f)
            {
                if (y > max) max = y;
            }
            Console.WriteLine("vertical bar Chart");
            for (i = max; i >= 1; i-- ) // i-- because max frequency will go dwon from 6 to 1
            {
                for (j = 0; j < f.Length; j++)
                {
                    if (f[j]<i) Console.Write("   ");
                    else Console.Write(" * ");
                }
                Console.WriteLine();
            }
            for (i = 11; i <= 20; i++ )
            {
                Console.Write("  " + i);
            }
                Console.ReadKey();



我应该如何处理水平条形图?我真的迷路了...

And how should I approach the horizontal bar chart? I'm really lost...

谢谢!

推荐答案

您认为到底是什么错?除了不符合数字的条形之外,它对我来说还不错.

What exactly do you think is wrong? Apart from the bars not lining up with the numbers, it looks right to me.


这篇关于对于循环任务,请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:30