1

当我为直方图运行我的程序时,它会运行,但它没有给出我想要的输出。我希望 输出是这个,但我得到了这个。如果可能的话,我还希望我的直方图打印为垂直输出,而不是水平输出。此外,我还想添加更多的垃圾箱。

PS:我拥有的 data.txt 文件包含以下内容

我的代码是: https ://pastebin.com/VyREurFP

BigInteger range = max.toBigInteger().subtract(min.toBigInteger());
        BigInteger maxWidth = new BigInteger("8");
        BigInteger width = new BigInteger("1");
        if (range.compareTo(maxWidth) == 1){
            width = range.divide(maxWidth);
            if (range.remainder(maxWidth).compareTo(BigInteger.ZERO)==1){
                width = width.add(BigInteger.ONE);
            }
        }
        int binCount = range.divide(width).intValue();
        if (range.remainder(width).compareTo(BigInteger.ZERO)==1){
            binCount++;
        }
        // binCount is the number of bins that will be used
        // Find the class interval for each bin
        List<String> classInterval = new ArrayList<>();
        BigInteger classStart = min.toBigInteger();
        for (int b = 0; b< binCount; b++){
            classInterval.add(String.format("%3s",classStart.toString())+" >= x < "+String.format("%3s",classStart.add(width).toString()));
            classStart = classStart.add(width);
        }
        // Fill each bin
        long[] bin = new long[binCount];
        BigDecimal bw = new BigDecimal(width);
        for (int i = 0; i< list.size(); i++){
            int binNumber = list.get(i).divide(bw,RoundingMode.HALF_UP).intValue()-1;
            bin[binNumber]++;
        }
        // Print out the details of each bin
        int total = 0;
        for (int b = 0; b< binCount; b++){
            System.out.print(classInterval.get(b)+" |");
            for (int d = 0; d<total; d++){
                System.out.print("#");
            }
            for (int v = 0; v< bin[b]; v++){
                System.out.print("*");
                total++;
            }
            for (long s = bin[b]; s<60;s++){
                System.out.print(" ");
            }
            System.out.println("("+bin[b]+")");
        }
4

0 回答 0