【问题标题】:Create bar graph using Java使用 Java 创建条形图
【发布时间】:2018-09-29 21:29:43
【问题描述】:

我正在处理一项使用 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中运行后无法编译。它说执行在控制台中终止。

【问题讨论】:

标签: java histogram bar-chart


【解决方案1】:

静态方法中只能使用静态变量。下面的代码编译正常:

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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2010-10-22
    • 2021-02-19
    • 2020-12-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    相关资源
    最近更新 更多