java - 这个数组如何打印这个条形图?

标签 java arrays

public class BarChart // Modified from Fig 7.6
{
    public static void main(String[] args)
    {
        int[] grades = {90, 71, 89, 75, 83, 87, 81, 100, 99, 83,
                65, 100, 91, 78, 88, 62, 55, 84, 73, 89}; // 20 elements
        int[] frequency = new int[11]; // 11 ranges 
        System.out.println("Grade distribution:");
        for (int i = 0; i < grades.length; i++) {
            frequency[grades[i]/10]++; //How does this work? why does it say 10
        }
        for (int i = 0; i < frequency.length; i++)
        {
            if (i == 10) {
                System.out.printf("%5d: ", 100);
            }
            else {
                System.out.printf("%02d-%02d: ", i * 10, i * 10 + 9);
            }
            for (int stars = 0; stars < frequency[i]; stars++) { // How does it know where to print the stars?
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

输出:

pic of output

我无法理解该程序的工作原理。我提出了一些问题作为评论,以获得更清晰的答案。我还附上了输出的图片。

最佳答案

我直接在代码中注释了:

public class BarChart // Modified from Fig 7.6
{
    public static void main(String[] args)
    {
        /*array that contains the data that will be used to display the distribution*/
        int[] grades = {90, 71, 89, 75, 83, 87, 81, 100, 99, 83,
            65, 100, 91, 78, 88, 62, 55, 84, 73, 89}; // 20 elements
        /*this frequecncy is used to divided your interval [0,100] in 11 slices*/
        int[] frequency = new int[11]; // 11 ranges 
        System.out.println("Grade distribution:");
        for (int i = 0; i < grades.length; i++) {
            /*division of your i element of grade array by 10, 
            10 here is used here to make correspond your grades 
            to your slices defined in the frequency variable, 
            (type int divided by 10 will give an int as output 
            (the decimals are truncated -> equivalent as taking the floor of the number) 
            so you have an int that can be used as an index for your frequency array, 
            the grades[i]/10 will be incremented by 1 -> this will allow to print the stars after*/
            frequency[grades[i]/10]++; 
        }
        for (int i = 0; i < frequency.length; i++) //loop that will do the display
        {
            if (i == 10) {
                System.out.printf("%5d: ", 100);//this will print the last line of your output, 
                //printf will not print the EOL char so the other printing operations are done on the same line
            }
            else {
                System.out.printf("%02d-%02d: ", i * 10, i * 10 + 9); 
                //this is will create your DD-DD intervals (00-09:, 10-19)
            }
            for (int stars = 0; stars < frequency[i]; stars++) { 
            // the value stored in frequency thanks to this operation: frequency[grades[i]/10]++; will be displayed,
            //you loop from 0 until you reach the value of frequency[i] and display a star at each loop
                System.out.print("*");
            }
            System.out.println();//this will print the end of line character
        }
    }
}

关于java - 这个数组如何打印这个条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47048734/

相关文章:

java - 参数中的 3 个点是什么?/什么是可变元数 (...) 参数?

Java - 使用 KeyPressed 更改另一个类中矩形的位置

javascript - 在 MIRTH 中导入自定义 JAR。 (2.2.1.5861)

java - 避免循环内使用 toCharArray() 或 arrayCopy 产生冗余

javascript - 使用 settimeout 绕过地理编码查询限制

arrays - 使用 Intent(in) 参数初始化 Fortran 参数

java - Spring 数据: how to use repository's inner interfaces outside the outer class?

java - 未找到 Servlet(GET+App Engine)

java - 闪存套接字未正确读取 Java 服务器

java - 无法将对象添加到非原始数组