java - 使用 Java 创建条形图
问题描述
我正在执行一项使用 Java 使用随机数创建条形图的任务,但我不知道如何正确编码,当我继续下一步时,它会不断出错。
public class BinSort {
final int N_BINS = 0; //number of bins
final int N_SAMPLES = 0; //total random integers
final float BIN_WIDTH = 0; //width of the bin
int [] nums; //generate and store random numbers
int [] binCount; //array
int max = 0; //largest random number = (max-1)
public void main(String[] args) {
int nBins, nSamples; //initializers
BIN_WIDTH = (float) (max/N_BINS); //calculate BIN_WIDTH
nums = new int[] {}; //initialize nums array
for (int i = 0; i < max; i++) {
int array = nums[i];
}
}
public void generateBins() {
int bin;
int [] binCount = new int [N_BINS]; //set binCount array with N_BINS elements
for (int i = 0; i < N_SAMPLES; i++) {
int array = binCount[i];
bin = (int) Math.floor(nums[i]/BIN_WIDTH);
}
}
public void printBins() {
float freq;
for(int i = 0; i < binCount.length; i++) {
freq = (binCount[i]/N_SAMPLES);
System.out.print(N_SAMPLES + " random integers in " + binCount + " sorted into " + N_BINS + " bins:");
float binMin = i * BIN_WIDTH;
float binMax = binMin + BIN_WIDTH;
System.out.println(binCount[i] + freq + binMin + binMax);
}
}
}
这段代码不完整,但我不知道下一步该做什么。所以,我被困住了。有人可以帮帮我吗?
编辑:程序在eclipse中运行后无法编译。它说执行在控制台中终止。
解决方案
静态方法中只能使用静态变量。下面的代码编译良好:
public class BinSort {
static final int N_BINS = 0; //number of bins
static final int N_SAMPLES = 0; //total random integers
static float BIN_WIDTH = 0; //width of the bin
static int [] nums; //generate and store random numbers
int [] binCount; //array
static int max = 0; //largest random number = (max-1)
public static void main(String[] args) {
int nBins, nSamples; //initializers
BIN_WIDTH = (float) (max/N_BINS); //calculate BIN_WIDTH
nums = new int[] {}; //initialize nums array
for (int i = 0; i < max; i++) {
int array = nums[i];
}
}
public void generateBins() {
int bin;
int [] binCount = new int [N_BINS]; //set binCount array with N_BINS elements
for (int i = 0; i < N_SAMPLES; i++) {
int array = binCount[i];
bin = (int) Math.floor(nums[i]/BIN_WIDTH);
}
}
public void printBins() {
float freq;
for(int i = 0; i < binCount.length; i++) {
freq = (binCount[i]/N_SAMPLES);
System.out.print(N_SAMPLES + " random integers in " + binCount + " sorted into " + N_BINS + " bins:");
float binMin = i * BIN_WIDTH;
float binMax = binMin + BIN_WIDTH;
System.out.println(binCount[i] + freq + binMin + binMax);
}
}
}
推荐阅读
- html - 无法使用 selenium webdriver (Java) 从 html 标签中获取属性
- python - CreateView保存表单后如何进入另一个页面
- android - android中targetSdkVersion的建议是什么?
- c# - 在 Visual Stuido 2019 中添加 lambda/func 箭头运算符 => 是否有任何快捷方式?
- php - 如何处理下拉列表中不再存在的现有下拉值?
- java - 无法将 Json 字符串转换为 Java 对象
- ios - 在 Xcode 11 和 iOS 13 上切换 UISwith 时应用卡住了
- mysql - SQL:为什么订票需要交易?
- html - Excel VBA to activate selectitem in web and go web for data
- postgresql - 比较表中的行对