2017-06-12 55 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限制。 –