java - 如何将数组排序到“箱”中,并打印出该箱中有多少个数字的星星?
问题描述
//所以基本上对于下面的所有内容,我正在尝试对已生成的随机数进行排序,然后“排序”然后放入垃圾箱中,然后对于垃圾箱中有多少个数字,将打印一个星 *出每个号码。最后它看起来像一个直方图。像这样:
[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();
}
}
}
解决方案
在你的构造函数中,你有
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
随机数并将它们存储在实例变量中。0
max
storeNumbers
接下来,在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
我认为这就是你要找的。
请务必将您的原始代码与上述更改进行比较,并确保您了解问题所在以及更改的原因。
推荐阅读
- java - Postman 和 RestTemplate 的 API 调用中的正文参数相同,但只有 Postman 有效
- xcode - 无法保存文档“xxx.dae”
- sql - SQL 中的简单 CASE 表达式
- sql - Oracle - 列是否附加了序列?
- php - 如何在 PHP 中获取当前服务器路径、url 域、文件名或脚本名
- gravity-forms-plugin - JS 提交按钮重力表单
- tensorflow - AttributeError:“顺序”对象没有属性“run_eagerly”
- sqlite - 将几个列值从一个表复制到另一个匹配的 ID - SQLite
- assembly - 如何使用 compare + je 计算匹配项?
- sql - 将一列不规则列拆分为多列