使用星号的Java直方图 [英] Java Histogram using asterisks

查看:127
本文介绍了使用星号的Java直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个利用应用程序运行程序的直方图类。

I am trying to create a histogram class that utilizes an application to run the program.

 public class Histogram
{
   String name;
   int max;


   public Histogram (String name, int max)
   {
      this.name = name;
      this.max = max;
   }

   int[] count = new int[max+1];

   public void add(int numbers) 
   {
    // Handles out of bounds case
    if (numbers < 0 || numbers > max)
        return;

    count[numbers]++;
   }

   public String toString()
   {
      String result = name + "\n";
      for (int index = 0; index < count.length; index++)
      {
      result = result + count[index] + ": ";
         for (int indexStar = 0; indexStar < count[index]; indexStar++)
            result = result + "*";
         result = result + "\n";
         }
         return result;
   }
}

我的申请:

public class HistogramDataCollector
{
   public static void main (String[] args)
   {
      Histogram h = new Histogram ("Favorite Number Survey", 11);

      h.add(1); h.add(0); h.add(9); h.add(7); h.add(7);
      h.add(3); h.add(4); h.add(6); h.add(5); h.add(2);
      h.add(2); h.add(8);

      System.out.println(h);
   }
}

我正在努力弄清楚如何添加int数字,以获得它们出现的频率,以制定用星号创建的直方图。
谢谢!

I am struggling to figure out how to add the int numbers to get the frequency of their occurrences to formulate a histogram created with asterisks. Thanks!

推荐答案

你需要做的第一件事就是摆脱那些无关的 for 循环代码,它不在您班级的所有方法之内。这将不允许您的代码编译。但是,一旦删除该代码,此类应该直接工作。您也可以删除数字数组,因为它没有在您的类中使用。只需使用计数变量,您就会明白为什么。

The first thing you need to do is get rid of that extraneous for loop code that is outside of all of your methods in your class. This won't allow your code to compile. However, once you remove that code, this class should work straight up. You can also get rid of the numbers array as it is not being used in your class. Just use the count variable, and you'll see why.

另外需要注意的是,您需要更改构造函数和变量定义,使它们看起来像这样:

Also another note, you need to change your constructor and your variable definitions so that they look like this:

String name;
int max;
int[] count;

public Histogram(String name, int max) 
{
   this.name = name;
   this.max = max;
   this.count = new int[max+1];  
}

原因是因为 max 被假定为 0 。因此,这将声明一个大小 1 的数组,并且当您在构造函数中读入 max 变量时它不会更改。因此,您需要将其保留为未分配,然后在构造函数中分配数组

The reason why is because max is assumed to be 0 when you create an object of this class. As such, this would declare an array of size 1, and it would not change when you read in the max variable in your constructor. As such, you need to leave this as unallocated, then allocate the array inside the constructor.

正如您所提到的,您对如何从此直方图类中获取其出现频率感到困惑。记住直方图的定义。直方图 是您在数据中看到的数字的频率。您班级中的计数数组将自动存储频率或您在数据集中看到该特定数字的次数。您使用 add()方法,此方法的输入是您要观察到的直方图中的数字。

As you mentioned, you are confused with how to get the frequency of their occurrences from this histogram class. Remember the definition of what a histogram is. The histogram is the frequency of numbers that you see in your data. The count array in your class will automatically store the frequency or how many times you see that particular number in your data set. You use the add() method and the input into this method is the number you are observing to be placed in your histogram.

作为直方图如何工作的一个例子,如果 count [0] = 1,count [1] = 2,count [2] = 4 ,这意味着你见过的次数 0 是1次, 1 2次, 2 数据集中有4次。因此,您只需迭代计数数组的每个元素以检索每个数字的频率。鉴于您在测试者类中的上述示例,在调用各种添加方法后,每个数字的频率计数将为: count [0] = 1,count [1] = 1,count [2] = 2,count [3] = 1,count [4] = 1,count [5] = 1,count [6] = 1,count [7] = 2,count [8] = 1,count [9] = 1,count [10] = 0

As an example with how a histogram works, if count[0] = 1, count[1] = 2, count[2] = 4, this means that the number of times you have seen 0 is 1 time, 1 2 times, and 2 4 times in your dataset. As such, you simply have to iterate over every element of your count array to retrieve the frequencies of each number. Given your example above in your tester class, after you call the various add methods, your frequency counts for each number would be: count[0] = 1, count[1] = 1, count[2] = 2, count[3] = 1, count[4] = 1, count[5] = 1, count[6] = 1, count[7] = 2, count[8] = 1, count[9] = 1, count[10] = 0.

计数中的每个位置您看到该数字的次数。因此,对于 add()方法,数字是您看到的数字。因此,您可以访问属于该数字的特定插槽,并将计数增加1.例如,执行 h.add(0); 将等效于 count [0] ++; count [0] = count [0] + 1; 相同。访问槽 0 ,并增加1.我们已经看到这个 1 时间。对于 h.add 的后续调用,我们会在额外的时间内看到该特定数字。

Each location in count is how many times you see that number. As such, for the add() method, numbers is the number that you are seeing. As such, you access that particular slot that belongs to that number, and increment the count by 1. For example, doing h.add(0); would then be equivalent to count[0]++; which is the same as count[0] = count[0] + 1;. Access slot 0, and increment by 1. We have seen this 1 time. For subsequent calls to h.add, we would see that particular number an additional time.

但是,您的添加方法不执行错误检查。具体来说,如果您指定的数字小于0或大于 max ,您将获得 OutOfBoundsException 尝试增加直方图的区间。因此,如果发生这种情况,我建议您只是从方法返回而不做任何事情。换句话说:

However, your add method does not perform error checking. Specifically, should you specify a number that is less than 0 or larger than max, you will get an OutOfBoundsException when you try to increment the bins of your histogram. As such, I would recommend you simply return from the method without doing anything should this happen. In other words:

public void add(int numbers) {
    // Handles out of bounds case
    if (numbers < 0 || numbers > max)
        return;

    count[numbers]++;
}

在你的班级定义中,做 int count [] = new int [max + 1]; 会自动将此数组的所有元素分配给0. max 将是您在此中看到的最高数字你的数据集。确保添加 1 来计算0索引,这就是它为 max + 1 插槽分配的原因。我们这样做是为了确保我们能够记录数据集中所见的每个可能数字的数字频率。当你从这个类创建一个对象时,所有这些二进制位将被重置为0,因此你可以立即开始使用该对象。

In your class definition, doing int count[] = new int[max+1]; will automatically assign all elements to this array to 0. max would be the highest possible number you see in your dataset. Make sure you add 1 to account for the 0 index, which is why it's allocating for max+1 slots. We do this to ensure that we are able to log the frequency of numbers for every possible number that is seen in your dataset. When you create an object from this class, all of those bins will be reset to 0, and so you can start using the object right away.

我想要注意的一件事是你说你想通过显示星号来表示bin计数来实现这个直方图。因此,您需要修改 toString()方法,如下所示:

One thing I would like to note is that you said you wanted to implement this histogram by showing asterisks to represent the bin counts. As such, you would need to modify your toString() method to be like this:

public String toString()
{
   String result = name + "\n";
   for (int index = 0; index < count.length; index++) {
       result = result + index + ": "; // New
       for (int indexStar = 0; indexStar < count[index]; indexStar++) // New
           result = result + "*"; // New
       result = result + "\n"; // New
   }
   return result;
}

以上代码将对您的直方图中的每个bin执行的操作,它将显示数字的频率计数,在后面放置一个冒号,一个空格,然后放置与该特定数字的频率一样多的星号。因此,如果你考虑我的第一个例子,你应该看到:

What the above code will do is that for each bin that is in your histogram, it will show the frequency count of the number, place a colon after, a space, then place as many asterisks as there are for the frequency of that particular number. Therefore, if you consider my first example, you should see:

0: *
1: **
2: ****

请注意,我只是显示频率计数的样子。我没有正确创建此类的对象,因此直方图的名称未定义。但是,这是你应该看到的直方图频率计数。

Note that I'm just showing what the frequency counts look like. I haven't properly created an object of this class, so the name of the histogram is undefined. However, this is what you should see for the histogram frequency counting.

希望这能解决你的问题!

Hope this solves your question!

这篇关于使用星号的Java直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆