首页 > 解决方案 > 使用 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中运行后无法编译。它说执行在控制台中终止。

标签: javahistogrambar-chart

解决方案


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

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

推荐阅读