2017-02-26 15 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

我建議添加一個名爲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])來選擇放置double值的括號,從而擺脫了多個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()); 
    } 
} 

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

輸出示例:

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

看來你只用一個變量來計算這個方法中數字的出現次數。這會導致程序顯示0有9次出現,其餘數字出現0次。我同意用戶David Choweller的評論,他建議你可以使用數組來解決這個問題。然而,另一個解決方案可能是一個HashMap,其中您將該號碼存儲爲密鑰,並將要打印的字符串作爲值存儲。然後,您可以像當前一樣使用循環結束數字,並打印出與其關聯的值。

+0

我只是想指出陣列解決方案可能更快,更高效,但我想分享當我看到此問題時首先想到的解決方案。 – UnknowableIneffable

+0

感謝您的反饋@UnknowableIneffible – nammrick