2012-07-23 119 views
0

我堆疊了一段時間。我嘗試過調試,但我找不出解決方案。我正在計算數字的發生。所以我的問題是,當我打印輸出是計算數組中出現的數字

3 occurs 1 times 
1 occurs 1 times 
0 occurs 1 times 
2 occurs 1 times 
1 occurs 2 times 
3 occurs 2 times 
2 occurs 2 times 
0 occurs 2 times 
10 occurs 1 times 
4 occurs 1 times 

,而不是

1 occurs 2 times 
0 occurs 2 times 
2 occurs 2 times 
3 occurs 2 time 
10 occurs 1 times 
4 occurs 1 times 

因此,如果數量已經超過1次出現的,應該說這一次沒有像很多倍,有發生。乾杯下面是代碼

import java.util.*; 

public class CountingOccuranceOfNumbers 
{ 

    public static void main(String[] args) 
    { 
     countNumbers(); 
    } 

    public static void countNumbers() 
    { 
     Scanner input = new Scanner(System.in); 
     Random generator = new Random(); 
     int[] list = new int[11]; 
     int[] counts = new int[150]; 
     int counter = 0; 
     int number = 1; 


     while(counter <= 10) 
     { 
       number = generator.nextInt(11); 
       list[counter] = number; 
       counter++; 
     } 
     for(int i=0; i<list.length - 1; i++) 
     { 
      counts[list[i]]++; 
//   System.out.print(list[i] + " "); 

      System.out.println(list[i] +" occurs " + counts[list[i]] + " times"); 
     } 

    } 

} 
+3

我建議您檢查一下您的筆記和課程文本。提示將在那裏找到。至於在SO上被卡住並尋求幫助。準確解釋你卡在哪裏,你試過了什麼,爲什麼它不起作用。然後添加一個特定的問題。但請先查看你的筆記。 – 2012-07-23 08:12:43

+0

如果有人無法分辨,這本來就是一個家庭作業問題,所以下面看到的一些答案將反映試圖給出原始海報提示的人,而不是僅僅給他/她一個完整的解決方案。請不要低估這些。請參閱[我如何問及回答作業問題?](http://meta.stackexchange.com/a/10812/163250)。 – 2014-04-14 05:56:13

回答

3

另一種選擇是番石榴的多集類,將跟蹤計數爲您提供:

int values[] = ...; 
Multiset<Integer> ms = HashMultiset.create(); 
ms.addAll(Ints.asList(list)); 

int count0 = ms.count(Integer.valueOf(0)); 
int count1 = ms.count(Integer.valueOf(1)); 

這裏,多集,HashMultiset和INTS都是番石榴類。

請注意,Multiset通過使用Map和Counter來跟蹤計數器,可以完成上面提到的某些功能。它只是從你身上抽象出來,使你的代碼更簡單。

3

使用HashMap<Integer>,<integer> ht來管理您的計數

if (ht.get(newNumber) == null) { 
    ht.put(newNumber, 1); 
} else { 
    ht.put(newNumber, ++ht.get(newNumber)); 
} 

更正HashTableHashMap++get(..)

+1

對於他有的問題,數組是最好的集合(並且Hashtable不會解決他的問題)即使它做了,HashMap(作爲它的未同步)或TreeMap(作爲它的排序)將是更好的選擇。 – 2012-07-23 08:21:43

+0

@PeterLawrey你的權利,我感謝地圖和寫表,我糾正我的帖子 – 2012-07-23 08:32:12

1

好吧,我會盡量給你提示或兩個。

  1. 由於您爲每個數字出現多次打印行,您可能不應該打印任何東西,直到您完成計數。
  2. 看起來你的輸出應該按照出現次數排序。如果是這種情況,保存數組中的計數可能不是最好的主意。考慮使用Map<Integer, Integer>代替,其中鍵是數字,並且值是出現次數。
2

你有一個循環來計算也給出了運行總數的發生。看來你想要的僅僅是當計數完成時打印總數。即它應該在另一個循環中。

+0

如果其他循環嵌套或獨立於此 – 2012-07-23 08:24:58

+0

你應該能夠解決這個問題,或自己嘗試。 – 2012-07-23 08:40:39

-1
import java.io.BufferedReader; 
    import java.io.InputStreamReader; 


    public class NumberRepetition { 



    public static void main(String[] args) throws Exception { 
     int size; 
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("enter size of array"); 
    size=Integer.parseInt(br.readLine()); 
    int el; 
    int[] a=new int[size]; 
    for(int i=0;i<size;i++) 
    { 
     System.out.println("enter a number"); 
     el=Integer.parseInt(br.readLine()); 
     a[i]=el; 
    } 
    for(int i=0;i<size;i++) 
    { 
     for(int j=0;j<size-1;j++) 
     { 
      if(a[j]>a[j+1]) 
      { 
       int temp=a[j]; 
       a[j]=a[j+1]; 
       a[j+1]=temp; 

      } 
     } 
    } 
    int count=0; 
    for(int i=0;i<size;i++) 
    { 
     for(int j=0;j<size;j++) 
     { 
      if(a[i]==a[j]) 
      { 
       count++; 

      } 



     } 
     System.out.println(a[i]+" \t " +"occurence time is"+"\t"+count); 
     if(count!=0) 
     { 
      i=i+count-1; 
     } 
     count=0; 
    } 

} 

} 
+1

設置你的代碼的格式,使它更容易閱讀和提供信息,這將有所幫助。 – 2013-12-03 13:41:08

0

創建一個HashMap並將新的Entry放入map中,其中value爲Integer。

如果您碰到相同的字符,則遞增與該鍵關聯的整數值。 o.w它是一個新的密鑰,並將該值設置爲1.

Integer entryValue; 
Map map = new HashMap(); 


    for (int i =0; i < s1.length(); i++) 
    { 
     entryValue = (Integer)map.get(s1.charAt(i)); 

     if (entryValue == null) 
     { 
      map.put(s1.charAt(i), new Integer(1)); 
     } 

     else 
     { 
       map.put(s1.charAt(i), new Integer(entryValue.intValue()+1)); 
     } 
    } 
相關問題