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

时间:2015-11-12 04:49:07

标签: java arrays arraylist histogram

我正在尝试从包含学生成绩的arrayList制作直方图。我已经按照此处显示的等级细分:

library(Matrix)
as.matrix(bdiag(Map(`*`,mget(letters[1:3]), v1)))
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
# [1,]    2    8   14    0    0    0    0    0    0
# [2,]    4   10   16    0    0    0    0    0    0
# [3,]    6   12   18    0    0    0    0    0    0
# [4,]    0    0    0    7    9    0    0    0    0
# [5,]    0    0    0    8   10    0    0    0    0
# [6,]    0    0    0    0    0   36   52   68   84
# [7,]    0    0    0    0    0   40   56   72   88
# [8,]    0    0    0    0    0   44   60   76   92
# [9,]    0    0    0    0    0   48   64   80   96

我对直方图的代码已经改变了几次,但需要包括下面列出的方法。我已经离开了我当前的代码,但我正在努力如何让直方图按列出的方式工作。

/**
 * 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;
}   

3 个答案:

答案 0 :(得分:0)

创建重复符号字符串的方法之一是使用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'

答案 1 :(得分:0)

感谢帮助人员。我实际上得到了一个有效的解决方案:

/**
 * 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 "";
}   

它打印出我正在寻找的东西。希望这有助于将来的某个人。

A:2

B:2

C:2

D:0

F:1

**

**

**

*

答案 2 :(得分:0)

为了您的兴趣和参考,这里是使用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);
}