有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何从一个Java数字重复数组中创建星号的柱状图?

我有一个重复数字的数组,我需要用“*”生成的“直方图”来显示它们。直方图应该如下所示:

            *
        *   *
  *     *   *
  *     * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
1 2 3 4 5 6 7 8 9 10

我有这个阵列

int[] repetition = new int[] {4,6,4,4,6,5,7,4,3,3};

我可以像这样水平打印:

1*****
2******
3****
4****
5******
6*****
7*******
8****
9***
10***

如何创建垂直直方图


共 (5) 个答案

  1. # 1 楼答案

    public static final void printHistogram(int[] coll) {
            int max = coll[0];
            for (int i : coll)
                max = i > max ? i : max;
            boolean[][] array = new boolean[max][coll.length];
            for (int i = coll.length - 1; i >= 0; i--) {
                for (int j = 0; j < coll[i]; j++) {
                    array[j][i] = true;
                }
            }
            for (int i = array.length - 1; i >= 0; i--) {
                boolean[] booleans = array[i];
                for (boolean b : booleans) {
                    System.out.print(b ? '*' : ' ');
                }
                System.out.println();
            }
    
            for (int i = 1; i <= array.length; i++) {
                System.out.print(i);
            }
        }
    

    这是意料之中的事

  2. # 2 楼答案

    听起来你有代码来“装箱”数据,有效地创建直方图。添加一些代码来跟踪所有箱子的最大计数。所以,在上面的例子中,最大值是8(从7号箱子算起)

    然后,设置一个阈值,从最大值开始,然后倒计时到1。在每次迭代中,在与达到或超过阈值的箱子对应的列中打印带有星号的行。所以在第一行,只有第7列会有一个星号,因为它是唯一一个符合当前阈值8的箱子。下一行(阈值7),第5列和第7列将显示星号。等等

    希望这足以帮助你自己编写代码

  3. # 3 楼答案

    Integer用于使用Collections max函数

    public static void main(String[] args) {
            Integer[] repetition = new Integer[] { 4, 6, 4, 4, 6, 5, 7, 4, 3, 3 };
            int maxValue = Collections.max(Arrays.asList(repetition));
            System.out.println("Maximum: " + maxValue);
            for (int i = maxValue; i > 0; i--) {
                for (int j = 0; j < repetition.length; j++) {
                    if (repetition[j] >= i) {
                        System.out.print(" * ");
                    } else {
                        System.out.print("   ");
                    }
                }
                System.out.println();
            }
            for (int j = 0; j < repetition.length; j++) {
                System.out.print(" " + (j + 1) + " ");
            }
        }
    
  4. # 4 楼答案

    我不会给你任何代码,但我可以给你一个开始的想法

    对于垂直柱状图,尽管看起来像是从上到下打印的,但事实并非如此;你就是不能垂直打印。使用循环(数组中的最高值为起始值,0为前哨值,每次迭代递减1),其中每次迭代代表直方图上的一条水平线,您应该确定需要在该线上打印哪个x标签的星号,对于那些在那一行上没有星号的标签,在那里放一个空格(用于格式化)。然后,创建另一个循环,并使用它列出底部的所有x标签

    希望这有帮助

  5. # 5 楼答案

    首先计算直方图的最大高度

    max=0;
    for(int i: repetition)
        if(i>max)
            max=i;
    

    然后,从上到下这样打印:

    for(j=max;j>=1;j--){                  //For each possible height of the histogram.
        for(k=0;k<repetition.length;k++)  //Check height of each element
            if(repetition[k]>=j)
                System.out.print("*");    //Print * if the k-th element has at least a height j.
            else
                System.out.print(" ");    //Else, do print blank space
        System.out.println();             //Newline after every row.
    }
    

    PS:这只是一个想法,不是一个完整的工作代码,它只适用于正高度值