Java 中成绩数组列表的直方图

Histogram from grade array list in Java

我正在尝试根据包含学生成绩的 arrayList 制作直方图。我已经做了成绩细分,如下所示:

/**
 * Returns a simple, 5-element array with the counts for each of the letter grades,
 * (A, B, C, D, and F), based on the 10-point scale
 * @return 5-element array
 */
private int[] calculateGradingBreakdown() {
    int[] breakdown;
    breakdown = new int[7];
    for (Student kids: this.students) {
        int grade = kids.getNumericGrade();
        if (grade >= 90) {
            breakdown[0] += 1;
        } else if (grade >= 80) {
            breakdown[1] += 1;
        } else if (grade >= 70) {
            breakdown[2] += 1;
        } else if (grade >= 60) {
            breakdown[3] += 1;
        } else {
            breakdown[4] += 1;
        }
    }
    return breakdown;
}

/**
 * Returns a string that lists the grade letter and the count of students
 * receiving the grade on the roster
 * @return grade breakdown
 */
public String getGradeBreakdown() {
    String gradeBreakdown = null;
    int[] breakdown = this.calculateGradingBreakdown();
    gradeBreakdown = ("A: " + breakdown[0] + "\nB: " + breakdown[1] + "\nC: " + breakdown[2]
            + "\nD: " + breakdown[3] + "\nF: " + breakdown[4]);
    return gradeBreakdown;
}

我的直方图代码已经更改了几次,但需要包括下面列出的方法。我已经保留了我当前的代码,但我正在努力解决如何让直方图按照列出的方式工作。

/**
 * Accepts a number of stars (*) to be created, creates a String with that
 * number of *'s side-by-side, and then returns that string.
 */
private String makeStarRow(int number) {
    int[] breakdown = this.calculateGradingBreakdown();
    number = breakdown[];
    String stars = 
}

/**
 * Returns a string holding a horizontal histogram of *'s
 */
public String getGradeHistogram() {
    String gradeHistogram = null;
    int[] breakdown = this.calculateGradingBreakdown();
    gradeHistogram = (this.makeStarRow(breakdown[0]));
    gradeHistogram += (this.makeStarRow(breakdown[1]));
    gradeHistogram += (this.makeStarRow(breakdown[2]));
    gradeHistogram += (this.makeStarRow(breakdown[3]));
    gradeHistogram += (this.makeStarRow(breakdown[4]));

    return gradeHistogram;
}   

成绩细分和直方图的输出应如下所示(数字根据另一个 class 中的输入):

A: 2
B: 2
C: 2
D: 0
F: 1

**
**
**

*

创建重复符号字符串的方法之一是使用 Arrays.fill:

private String makeStarRow(int number) {
    char[] starChars = new char[number];
    Arrays.fill(starChars, '*');
    String stars = new String(starChars) + '\n';
    return stars;
}

请注意,根据 getGradeHistogram 方法,您可能需要将 '\n' 附加到星星字符串的末尾。

感谢大家的帮助。我实际上得到了一个可行的解决方案:

/**
 * Accepts a number of stars (*) to be created, creates a String with that
 * number of *'s side-by-side, and then returns that string.
 */
private String makeStarRow(int number) {
    while (number > 0) {
        System.out.print("*");
        number--;
    } 
    if (number < 1) {
        System.out.println();
    }
    return null;
}

/**
 * Returns a string holding a horizontal histogram of *'s
 * @return histogram
 */
public String getGradeHistogram() {
    int[] histogram = this.calculateGradingBreakdown();
    for (int xi = 0; xi < this.students.size(); xi++) {
        int meh = histogram[xi];
        this.makeStarRow(meh);
    }
    return "";
}   

它打印出我要找的东西。希望这对以后的人有所帮助。

一个:2

B: 2

C: 2

D: 0

F: 1

**

**

**

*

为了您的兴趣和参考,这里有一个使用 Java 8 个流的解决方案:

void printHistogram(List<Integer> scores) {
    scores.stream().collect(Collectors.groupingBy(n -> n < 60 ? 5 : n / 10));
        .entrySet().stream().sorted(Map.Entry::comparingByKey)
        .map(entry -> entry.getValue().size());
        .map(size -> Stream.iterate(() -> "*").limit(size).collect(Collectors.joining()))
        .forEach(System.out::println);
}