2017-02-26 8 views
0

配列内の値の出現ごとにアスタリスクのヒストグラムを生成する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(); 
    } 
} 

どうすればこの問題を解決できますか?

+0

が見えます。 1つのカウントで複数の値をどのように追跡すると思いますか? –

+1

countStrings []という新しい文字列を追加することをお勧めします。要素0は1未満の値の数を追跡し、要素1は2未満の値の数を追跡します。各if条件のコードで、アスタリスクを配列の適切な要素に追加します。たとえば、 'array [i]> = 3 && array [i] <4'の場合、' countStrings [3] + = "*"; ' –

+0

というステートメントを実行すると、 Math.floor'関数を呼び出して、if-then-else文をすべて削除します。 –

答えて

0

この解決策では、(int) Math.floor(array[i])を使用して二重値を入れるブラケットを選択し、複数のif-then-elseステートメントを取り除きます。また、Stringの代わりにStringBuilderを使用して、アスタリスクの繰り返し連結をより効率的にするようにしました。

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()); 
    } 
} 

テストmain方法:

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); 
} 

出力例:あなたが唯一の1つのカウントを維持しているよう

00: ******* 
01: ******** 
02: *********** 
03: ************ 
04: ******** 
05: ********** 
06: ******* 
07: ******** 
08: ********** 
09: ************ 
10: ******* 
0
public static void drawHistogram(double[] array) { 

     String count[] = new String[array.length]; 

     for (int i = 0; i < array.length; i++) { 
      if (array[i] >= 0 && array[i] < 1) { 
       count[0] = "*"; 
      } else if (array[i] >= 1 && array[i] < 2) { 
       count[1] = "*"; 
      } else if (array[i] >= 2 && array[i] < 3) { 
       count[2] = "*"; 
      } else if (array[i] >= 3 && array[i] < 4) { 
       count[3] = "*"; 
      } else if (array[i] >= 4 && array[i] < 5) { 
       count[4] = "*"; 
      } else if (array[i] >= 5 && array[i] < 6) { 
       count[5] = "*"; 
      } else if (array[i] >= 6 && array[i] < 7) { 
       count[6] = "*"; 
      } else if (array[i] >= 2 && array[i] < 8) { 
       count[7] = "*"; 
      } else if (array[i] >= 2 && array[i] < 9) { 
       count[8] = "*"; 
      } else if (array[i] >= 9 && array[i] < 10) { 
       count[9] = "*"; 
      } else if (array[i] >= 10 && array[i] < 11) { 
       count[10] = "*"; 
      } 
     } 
     for (int j = 0; j <= 10; j++) { 
      System.out.print(j + count[j]); 
      System.out.println(); 
     } 
} 
+0

ご回答ありがとうございました – nammrick

0

このメソッドでは、1つの変数のみを使用して数値の数をカウントしているようです。この結果、0は9回の出現を示し、残りの数字は0回の出現を示します。私はあなたがこの問題を解決するために配列を使うことができると示唆したコメントのDavid Chowellerユーザーに同意します。しかし、別の解決策は、数値をキーとして格納するHashMapと、値として出力する文字列です。次に、現在のように最後の数字にループを使用して、それらに関連付けられた値を出力することができます。

+0

アレイソリューションがより高速で効率的かもしれませんが、私はこの問題を見たときに最初に思いついたソリューションを共有したいと考えていました。 – UnknowableIneffable

+0

フィードバックありがとう@UnknowableInefible – nammrick

関連する問題