2017-06-12 57 views
0

這裏我的代碼至今:計數頻率在一個HashSet的一些已經被拾取

public void lottoGame() {   
     HashSet<Integer> dups = new HashSet<Integer>(); 

     for(int howMany = 1; howMany <= 6; howMany++) { 
      int lottoNumber = lottoNum.nextInt(49) + 1; 

      while(dups.contains(lottoNumber)) { 
       lottoNumber = lottoNum.nextInt(49) + 1; 
      } 

      dups.add(lottoNumber); 
      randomLotto[howMany] = lottoNumber; 

      System.out.println(lottoNumber); 

    } 
    for(int counter : dups) { 
     numberCount[counter]++; //to store the counting of random numbers 
    } 

    for(int counting = 1; counting < numberCount.length; counting++) { 
     System.out.println(counting + " occurs " + numberCount[counting] + " times"); 
    } 
} 

所以基本上,我所做的就是創建一個HashSet,並把在6張隨機數。在我最近的2個for-loops中,我試圖計算出多少次繪製哪個數字並打印出來。問題在於,無論出於何種原因,打印出來時都以第三個字段開頭。有誰知道爲什麼?

+1

如果你不是來開始你的最後'for'循環在0? – khriskooper

+0

如果我開始第一個for循環與0它會給我7個數字而不是6,我不想有。嘗試使用最後一個for循環,它仍然在字段3開始。這裏有一個截圖:http://puu.sh/whYKh/46b2172585.png – TheSTARplow

+0

當你說在字段3開始,你的意思是打印「3發生...「並跳過」1發生...「和」2發生...「?我覺得這不太可能。 –

回答

0

我會建議使用的地圖是簡單得多這裏看看下面這個例子

public static void lotoGame() { 
    Random rand = new Random(); 
    Map<Integer, Integer> loto = new HashMap<>(); 
    while (loto.keySet().size() < 6) { 
     int lottoNumber = rand.nextInt(49) + 1; 
     loto.compute(lottoNumber, (key, value) -> value == null ? 1 : ++value); 
    } 

    loto.forEach((key,value)->{ 
     System.out.println(key + " occurs " + value + " times"); 
    }); 
} 
+0

我之前沒有和hashmaps一起工作過,但遲早我得看看他們,非常感謝你,我可能會使用你的代碼! – TheSTARplow

+0

高興地幫助,如果這個答案已經解決了你的問題,請考慮通過點擊複選標記來接受它。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做 – urag

1

這是您給出的例子。我輸出的結果是正確的。驗證您可能無法向我們顯示的設置或其他代碼。

看看:Is there a limit on the maximum of number of lines which can be printed to console by BlueJ?

class Test 
{ 
    public void lottoGame() 
    { 
     int[] randomLotto = new int[50]; 
     int[] numberCount = new int[50]; 
     Random lottoNum = new Random(); 
     HashSet<Integer> dups = new HashSet<Integer>(); 

     for (int howMany = 1; howMany <= 6; howMany++) 
     { 
      int lottoNumber = lottoNum.nextInt(49) + 1; 

      while (dups.contains(lottoNumber)) 
      { 
       lottoNumber = lottoNum.nextInt(49) + 1; 
      } 

      dups.add(lottoNumber); 
      randomLotto[howMany] = lottoNumber; 

      System.out.println(lottoNumber); 

     } 
     for (int counter : dups) 
     { 
      numberCount[counter]++; //to store the counting of random numbers 
     } 
     for (int counting = 1; counting < numberCount.length; counting++) 
     { 
      System.out.println(counting + " occurs " + numberCount[counting] + " times"); 
     } 
    } 

    public static void main(String[] args) 
    { 
     new Test().lottoGame(); 
    } 
} 

輸出:

43 
27 
38 
30 
14 
8 
1 occurs 0 times 
2 occurs 0 times 
3 occurs 0 times 
4 occurs 0 times 
5 occurs 0 times 
6 occurs 0 times 
7 occurs 0 times 
8 occurs 1 times 
9 occurs 0 times 
10 occurs 0 times 
11 occurs 0 times 
12 occurs 0 times 
13 occurs 0 times 
14 occurs 1 times 
15 occurs 0 times 
16 occurs 0 times 
17 occurs 0 times 
18 occurs 0 times 
19 occurs 0 times 
20 occurs 0 times 
21 occurs 0 times 
22 occurs 0 times 
23 occurs 0 times 
24 occurs 0 times 
25 occurs 0 times 
26 occurs 0 times 
27 occurs 1 times 
28 occurs 0 times 
29 occurs 0 times 
30 occurs 1 times 
31 occurs 0 times 
32 occurs 0 times 
33 occurs 0 times 
34 occurs 0 times 
35 occurs 0 times 
36 occurs 0 times 
37 occurs 0 times 
38 occurs 1 times 
39 occurs 0 times 
40 occurs 0 times 
41 occurs 0 times 
42 occurs 0 times 
43 occurs 1 times 
44 occurs 0 times 
45 occurs 0 times 
46 occurs 0 times 
47 occurs 0 times 
48 occurs 0 times 
49 occurs 0 times 
+0

奇怪的是,我將你的代碼複製到一個新類中,並試用了它,仍然以3開始。也許它是BlueJ的一個問題? – TheSTARplow

+0

很可能您的控制檯設置方式存在問題。 –

+0

看看我在答案中添加的帖子。他們說如何消除BlueJ限制。 –