2017-05-07 90 views
0

所以我創建非常簡單的函數來計算重複單詞的概率,但我發現概率數組並沒有被初始化?總是例如概率由0 :概率數學總是輸出零?

String original = "abcabd"; 
String n = "abcd"; 
int count = 0; 
float Probabilty[] = new float[n.length()]; 

for(int i = 0 ; i < n.length() ; i++){ 
    for(int j =0 ; j < original.length() ; j++){ 
     if (n.charAt(i)== original.charAt(j)){ 
      count = count +1; 
     } 
    } 
    Probabilty[i] = count/original.length(); 
    System.out.println(Probabilty[i]); 
    count = 0; 
} 
+1

陣列不是問題。使用調試器。製作[mcve] –

回答

1

它與線做:

Probabilty[i] = count/original.length(); 

兩個countoriginal.length()是整數,所以將所述兩種用途整數除法。如果轉換一個float或double,那麼你會得到適當的分工,就像這樣:

Probabilty[i] = ((float)count)/original.length(); 

了類似的問題,請參見How can I convert integer into float in Java?