如何垂直打印与某些数字对应的星星数量?

How to print number of stars corresponding to some numbers vertically?

我正在尝试制作垂直显示的直方图,但我的输出给出了不正确的模式。

输入:

unqNums = [0.0, 2.0, 1.0, 5.0]
repeated = [2, 2, 1, 1]

输出:

*    *
*    *    *    *
0.0, 2.0, 1.0, 5.0

我的代码:

 System.out.println(Arrays.toString(gradesNoRepead));
 //[0.0, 2.0, 1.0, 5.0]
 System.out.println(Arrays.toString(repeatedVal));
 //[2, 2, 1, 1]
//getting the max rep
        int mxRep = Main.getMax(repeatedVal);
        for(int i = mxRep; i > 0; --i){
            for(int l=0; l<gradesNoRepead.length; ++l){
        
                System.out.print((gradesNoRepead[l] >= i) ? " * " : "  ");
            }
            System.out.println();
        }
        for (int m = 0; m < gradesNoRepead.length; m++) {
            System.out.print(" " + gradesNoRepead[m] + " ");
    }
    

你的问题是,在输出 * 值的循环中,你正在迭代并测试错误数组 (gradesNoRepead) 中的值,你应该使用 repeatedVal:

for(int l=0; l<repeatedVal.length; ++l){
    System.out.print((repeatedVal[l] >= i) ? "  *  " : "     ");
}

请注意,您还需要在输出字符串中添加一些空格,以使其与 gradesNoRepead 值的宽度正确匹配。