Java 直方图-等级分布

Java 直方图-等级分布,java,charts,histogram,Java,Charts,Histogram,提供了一个等级文本文件,该文件每行有一个整数,表示 学生在班上的成绩。这些数字没有排序,但它们被限制在0到100之间 包含全部费用使用数组时,必须计算每个等级值的频率并将其打印到 标准输出为水平直方图。还必须标记每个直方图条的范围和 允许用户指示他们希望直方图的大小间隔 这是我的作业,我被困在如何标记每个直方图条的范围和 允许用户指示他们希望直方图的大小间隔 如何修复我的代码 我的代码: import java.util.Scanner; import java.io.FileInputStre

提供了一个等级文本文件,该文件每行有一个整数,表示 学生在班上的成绩。这些数字没有排序,但它们被限制在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();
            }
        }
    }
}
行动!请注意,您当前正在循环中重用变量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]++;
    }
Enter the interval for the histogram (e.g. '0 100'):
5 11

HISTOGRAM    
Grade     Frequency
5         ***
6         *
7         ******
8         *
9         **
10        ******
11        ****
请注意,我没有包含任何意外条目的错误处理,如负边界、开始>结束或大于数组大小的上限;我让你决定

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

    // 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]++;
    }
Enter the interval for the histogram (e.g. '0 100'):
5 11

HISTOGRAM    
Grade     Frequency
5         ***
6         *
7         ******
8         *
9         **
10        ******
11        ****

我能说清楚吗?你还有什么问题吗

谢谢!!这真的很有帮助!