2014-01-30 24 views
-1

我有一個數組包含以下數字: 1,3,4,5,6,3,2,4,6,7,9,4, 12,3,4,6,8,9,7,6,43,2,4,7,7,5,2,1,3,4,6,311,1 我需要打印數組中的每個數字和它在數組中重複的次數。 運行以下程序時出現錯誤「我無法解析爲變量」。打印陣列中的每個數字及其重複的次數


public class PrintNosandRepeatation { 

    public static void main(String[] args) { 

     int a[] = new int[] {1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,7,5,2,1,3,4,6,311,1}; 
     for (int i=0; i< a.length; i++); 
     System.out.println(a[i]); 
    } 
} 

請讓我知道我錯了..

回答

4

刪除;if條件:

for (int i=0; i< a.length; i++); 
           ^------------- delete this 

爲什麼?因爲它聲明瞭一個空的語句,所以for正文將是空的。因此,當您嘗試使用變量i時,它實際上超出了它聲明的塊(塊由{ }分隔)。

+0

有說謊的錯誤..現在解決..你能告訴我我如何找到重複的nos.say我想顯示「1-重複3次」等。 – user3220565

+0

這應該是一個單獨的問題,但是當你問(你嘗試過什麼,有什麼問題)時,你肯定會表現出一些努力。 – Christian

相關問題