首页 > 解决方案 > 如何将数组排序到“箱”中,并打印出该箱中有多少个数字的星星?

问题描述

//所以基本上对于下面的所有内容,我正在尝试对已生成的随机数进行排序,然后“排序”然后放入垃圾箱中,然后对于垃圾箱中有多少个数字,将打印一个星 *出每个号码。最后它看起来像一个直方图。像这样:

[0, 10) 中的 12 个随机整数排序到 2 个 bin 中:

******* 7 0.5833333 [5.0, 10.0)

***** 5 0.41666666 [0.0, 5.0) 但它就像它跳过最后两个方法一样 - generateBins 和 printBins。我将如何根据数字(如上)将随机数分类到 bin 中,并为该数组 bin 中的每个数字打印一个 *?

public class BinSort {
    final int totalBins;
    final int totalRandom;
    final float widthBin;
    int [] storeNumbers;
    int [] binCount;

public BinSort (int nBins, int nSamples, int max) {
    totalBins = nBins; //total # of bins, ie 2
    totalRandom = nSamples; //total random number generated, ie 12
    widthBin = (float) (max/totalBins); ie 2


    int [] storeNumbers = new int [max];
    for (int i = 0; i < totalRandom-1; i++) {
storeNumbers[i] = Random.rand(i, max);
System.out.println(storeNumbers[i]);
}
}
void generateBins () {
    int [] binCount = new int [totalBins];
    for (int i=0; i < totalRandom-1; i++) {
    int bin = (int)(storeNumbers[i]/ totalBins);
    Math.floor(bin);
    bin  = binCount [i];
    }
}

void printBins () {
for (int i = 0; i < binCount.length - 1; i++) {
        for (int j=0; j < binCount[j]; j ++) {
           System.out.print("*");
           System.out.println(); }
    float freq = (binCount[i]/totalRandom);
    float binMin = (i * widthBin);
    float binMax = (binMin * widthBin);
    System.out.print(binCount[i] + freq + binMin + binMax);
    System.out.println();
    }
  }
}

标签: javaarrayssortingbingenerate

解决方案


在你的构造函数中,你有

int [] storeNumbers = new int [max];

这里的问题是,这将创建一个与您的实例变量同名的新局部变量storeNumbers。此外,大小应该是totalRandom,而不是max。您需要创建一个Random用于生成随机数的对象。把这些放在一起,我们得到:

public BinSort (int nBins, int nSamples, int max) {
  totalBins = nBins; //total # of bins, ie 2
  totalRandom = nSamples; //total random number generated, ie 12
  widthBin = (float) (max/totalBins); //ie 2

  storeNumbers = new int [totalRandom];
  Random rand = new Random();  
  for (int i = 0; i < totalRandom; i++) {
    storeNumbers[i] = rand.nextInt(max);
  }
}

这将在和(不包括)之间生成totalRandom随机数并将它们存储在实例变量中。0maxstoreNumbers

接下来,在generateBins你有同样的问题

int [] binCount = new int [totalBins];

这将再次隐藏您的实例变量binCount。astoreNumber落入的 bin 将由 给出(int)(storeNumbers[i] / widthBin),您需要将结果 bin 增加 1。

void generateBins()
{
  binCount = new int[totalBins];
  for (int i = 0; i < totalRandom; i++)
  {
    int bin = (int)(storeNumbers[i] / widthBin);
    binCount[bin] += 1;
  }
}

最后,到垃圾箱的打印。这条线

for (int j=0; j < binCount[j]; j ++) 

应该

for (int j=0; j < binCount[i]; j ++) 

此外,您应该使用printf来格式化要打印的数字。

void printBins()
{
  for (int i = 0; i < binCount.length; i++)
  {
    for (int j = 0; j < binCount[i]; j++)
    {
      System.out.print("*");
    }
    float freq = (float)binCount[i] / totalRandom;
    float binMin = i * widthBin;
    float binMax = (i+1) * widthBin;
    System.out.printf(" %d %.3f %.3f %.3f\n", binCount[i], freq, binMin, binMax);
  }
}

测试:

public static void main(String[] args)
{
  BinSort bs = new BinSort(2, 12, 10);
  bs.generateBins();
  bs.printBins();
}

输出:

***** 5 0.417 0.000 5.000
******* 7 0.583 5.000 10.000

我认为这就是你要找的。

请务必将您的原始代码与上述更改进行比较,并确保您了解问题所在以及更改的原因。


推荐阅读