Java 直方图的循环结构

Java 直方图的循环结构,java,arrays,loops,methods,histogram,Java,Arrays,Loops,Methods,Histogram,我正试图编写一个Java程序,为数组中每次出现的值生成一个星号直方图 如果元素分别为0,1,2,3,4,5,6,7,8,9,则每次出现时输出应有一个星号。比如说, 0:* 1:* 2:* 3:* 4:* 5:* 6:* 7:* 8:* 9:* 然而,我的输出是 0:********** 1: 2: 3: 4: 5: 6: 7: 8: 9: 下面的代码是我自己的 public static void drawHistogram(double[] array) { String cou

我正试图编写一个Java程序,为数组中每次出现的值生成一个星号直方图

如果元素分别为0,1,2,3,4,5,6,7,8,9,则每次出现时输出应有一个星号。比如说,

0:*
1:*
2:*
3:*
4:*
5:*
6:*
7:*
8:*
9:*
然而,我的输出是

0:**********
1:
2:
3:
4:
5:
6:
7:
8:
9:
下面的代码是我自己的

public static void drawHistogram(double[] array) {

    String count = "";

    for (int i = 0; i < array.length; i++) {
        if (array[i] >= 0 && array[i] < 1) {
            count += "*";
        } else if (array[i] >= 1 && array[i] < 2) {
            count += "*";
        } else if (array[i] >= 2 && array[i] < 3) {
            count += "*";
        } else if (array[i] >= 3 && array[i] < 4) {
            count += "*";
        } else if (array[i] >= 4 && array[i] < 5) {
            count += "*";
        } else if (array[i] >= 5 && array[i] < 6) {
            count += "*";
        } else if (array[i] >= 6 && array[i] < 7) {
            count += "*";
        } else if (array[i] >= 2 && array[i] < 8) {
            count += "*";
        } else if (array[i] >= 2 && array[i] < 9) {
            count += "*";
        } else if (array[i] >= 9 && array[i] < 10) {
            count += "*";
        } else if (array[i] >= 10 && array[i] < 11) {
            count += "*";
        }
    }
    for (int j = 0; j <= 10; j++) {
        System.out.print(j + count);
        count = "";
        System.out.println();
    }
}
publicstaticvoiddrawshistogram(双[]数组){
字符串计数=”;
for(int i=0;i=0&&数组[i]<1){
计数+=“*”;
}else如果(数组[i]>=1&&array[i]<2){
计数+=“*”;
}else如果(数组[i]>=2&&array[i]<3){
计数+=“*”;
}else如果(数组[i]>=3&&array[i]<4){
计数+=“*”;
}else如果(数组[i]>=4&&array[i]<5){
计数+=“*”;
}else如果(数组[i]>=5&&array[i]<6){
计数+=“*”;
}else如果(数组[i]>=6&&array[i]<7){
计数+=“*”;
}else如果(数组[i]>=2&&array[i]<8){
计数+=“*”;
}else如果(数组[i]>=2&&array[i]<9){
计数+=“*”;
}else如果(数组[i]>=9&&array[i]<10){
计数+=“*”;
}else如果(数组[i]>=10&&array[i]<11){
计数+=“*”;
}
}
对于(int j=0;j
publicstaticvoiddrawhistogram(双[]数组){
字符串计数[]=新字符串[array.length];
for(int i=0;i=0&&数组[i]<1){
计数[0]=“*”;
}else如果(数组[i]>=1&&array[i]<2){
计数[1]=“*”;
}else如果(数组[i]>=2&&array[i]<3){
计数[2]=“*”;
}else如果(数组[i]>=3&&array[i]<4){
计数[3]=“*”;
}else如果(数组[i]>=4&&array[i]<5){
计数[4]=“*”;
}else如果(数组[i]>=5&&array[i]<6){
计数[5]=“*”;
}else如果(数组[i]>=6&&array[i]<7){
计数[6]=“*”;
}else如果(数组[i]>=2&&array[i]<8){
计数[7]=“*”;
}else如果(数组[i]>=2&&array[i]<9){
计数[8]=“*”;
}else如果(数组[i]>=9&&array[i]<10){
计数[9]=“*”;
}else如果(数组[i]>=10&&array[i]<11){
计数[10]=“*”;
}
}

对于(int j=0;j在这种方法中,您似乎只使用一个变量来计算数字的出现次数。这导致您的程序显示0有九次出现,其余数字有0次出现。我同意用户David Choweller在评论中的意见,他建议您可以使用数组来解决此问题问题。但是,另一种解决方案可能是HashMap,其中您将数字存储为键,并将要打印的字符串存储为值。然后,您可以像当前一样使用循环遍历末尾的数字,并打印与它们相关的值。

此解决方案使用
(int)Math.floor(array[i])
选择要放入双精度值的括号,从而消除多个if-then-else语句。我还使用了StringBuilder而不是String,以使星号的重复串联更加有效

public static void drawHistogram(double[] array) {

    StringBuilder histoGram[] = new StringBuilder[11];
    for (int i = 0; i < histoGram.length; i++) {
        histoGram[i] = new StringBuilder();
    }

    for (int i = 0; i < array.length; i++) {
        int bracket = (int) Math.floor(array[i]);
        if (bracket >= 0 && bracket < histoGram.length) {
            histoGram[bracket].append("*");
        }
    }
    for (int j = 0; j < 11; j++) {
        System.out.format("%02d: %s\n", j, histoGram[j].toString());
    }
}
样本输出:

00: *******
01: ********
02: ***********
03: ************
04: ********
05: **********
06: *******
07: ********
08: **********
09: ************
10: *******

看起来您只保留了一个计数。您希望如何仅使用一个计数跟踪多个值?我建议添加一个名为countStrings[]的新字符串数组其中,元素0跟踪小于1的值的计数,元素1跟踪小于2的值的计数,依此类推。在每个
if
条件的代码中,您应该在数组中的相应元素上附加一个星号。例如,if
array[i]>=3&&array[i]<4
,您可以执行语句
countStrings[3]+=“*”;
您可以使用
Math.floor
函数进一步简化代码,并删除所有if-then-else语句。类似于
countStrings[Math.floor(array[i])+=“*”;
Math.floor
只是将一个double转换成下一个最小的整数。所以
Math.floor(3.5)
将是3。我只是想指出阵列解决方案可能更快、更高效,但我想分享我看到这个问题时首先想到的解决方案。感谢@UnknowableInAffible的反馈感谢您的回复
public static void main(String args[]) {
    double[] testValues = new double[100];
    for (int i = 0; i < 100; i++) {
        testValues[i] = Math.random() * 11.0;
    }
    drawHistogram(testValues);
}
00: *******
01: ********
02: ***********
03: ************
04: ********
05: **********
06: *******
07: ********
08: **********
09: ************
10: *******