【问题标题】:Histogram - Grade Distribution直方图 - 成绩分布
【发布时间】:2014-01-29 10:00:28
【问题描述】:

提供了一个成绩文本文件,每行有一个整数,代表一个 学生在课堂上的成绩。这些数字未排序,但它们的范围在 0 到 100 之间 (包括的)。使用数组,您必须计算每个等级值的频率并将其打印到 标准输出为水平直方图。您还必须标记每个直方图条的范围,并 允许用户指明他们希望制作直方图的大小区间。

这是我的任务,我被困在如何标记每个直方图条的范围和 允许用户指示他们希望使用什么大小的间隔来制作直方图?

如何修复我的代码?

我的代码:

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class GradeHistogram{

    public static void main(String[] args) throws Exception {
        Scanner gradeFile = new Scanner(new FileInputStream("grades.txt"));
        int counter = 0;
        while (gradeFile.hasNextLine()) {
        gradeFile.nextLine();
        counter++;
        }
        int[] grades = new int[counter];
        System.out.println("Grades loaded!");
        System.out.println("What bucket size would you like?");
        Scanner output = new Scanner(System.in);
        for (int i = 0; i < grades.length; i++) {
           grades[i] = Integer.parseInt(output.nextLine());
            for ( i = 0; i < grades.length; i++) {
            if (grades[i] > 0){
                System.out.print(i + "  | ");
                for (int j = 0; j < grades[i]; j++) {
                    System.out.print("[]");
                }
                System.out.println();
            }
        }
    }
}

【问题讨论】:

    标签: java charts histogram


    【解决方案1】:

    Ops! 请注意,您当前正在循环内重用变量i。因此,您的代码可能(并且可能)表现出意外。我也相信你没有正确解析文件。

    您可以使用辅助数组来存储每个等级的频率。初始化一个空数组(用零填充),然后为每个年级更新相应的插槽。

    代码是这样的:

        // 1. Generate your counting array
        //    (give it 101 slots to include all grades from 0 to 100)
        int gradeFrequency = new int[101];
        for (int j = 0; j < 101; j++) {
            gradeFrequency[j] = 0;
        }
    
        // 2. Parse the file and store the frequency of each grade
        while (gradeFile.hasNextLine()) {
           // get the grade and add 1 to its counter
            int grade = Integer.parseInt(gradeFile.nextLine());
            gradeFrequency[grade]++;
        }
    

    之后,您的 gradeFrequency 数组将包含 0 到 100 范围内每个等级的频率。

    打印直方图时,您可以要求用户输入他们希望查看直方图的区间,然后从数组中打印相应的频率。代码如下:

        // Retrieve the interval the user wants to see
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the interval for the histogram (e.g. '0 100'):");
        int lowerBound = s.nextInt();
        int upperBound = s.nextInt();
    
        // Print the histogram title and 'column headers'
        System.out.println();
        System.out.println("HISTOGRAM");
        System.out.println("Grade\tFrequency");
    
        // For each grade present in the interval, print out its 'label'
        // (the assigned grade)
        for (int i = lowerBound; i < upperBound; i++) {
            System.out.print(i + "\t");
    
            // For each time this grade was assigned to a student,
            // print an asterisk
            for (int j = 0; j < gradeFrequency[i]; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    

    (请注意,我没有对意外条目进行任何错误处理,例如负边界、开始 > 结束或大于数组大小的上限;我把它留给你!)

    示例执行此代码后的控制台将如下所示:

    Enter the interval for the histogram (e.g. '0 100'):
    5 11
    
    HISTOGRAM    
    Grade     Frequency
    5         ***
    6         *
    7         ******
    8         *
    9         **
    10        ******
    11        ****
    

    我能说清楚吗?或者您还有其他问题吗? :)

    【讨论】:

    • 谢谢!!真的很有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 2018-10-30
    相关资源
    最近更新 更多